我很困惑为什么这个函数的第一个和第三个版本会给出这个错误,而第二个定义工作正常。
-- head and tail
third :: [a] -> a
third [a] = head (tail (tail[a]))
-- Pattern matching
third2 :: [a] -> a
third2 (_:_:x:_) = x
-- List indexing
third3 :: [a] -> a
third3 [a] = [a]!!2
提前致谢
奇怪的是,第二个没有抱怨不详尽
模式,因为third2
不会匹配长度为零、一或二的列表。
这third
and third3
函数抱怨是因为[a]
不是一个变量,
这是一种模式。[a]
脱糖至(a:[])
,所以你可以把它们写成
third (a:[]) = head (tail (a:[]))
third3 (a:[]) = (a:[]) !! 2
这两种方法都不起作用,因为它们都是单元素列表。
我怀疑你想要的是
third a = head (tail a)
third3 a = a !! 2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)