我有一个函数的问题,该函数应该只返回列表的尾部.函数是myTail,即使输入是空列表,也应该给出一个可用的结果.
我想了解所有3种方式:模式匹配,保护方程和条件表达式
这工作:
> myTail_pat :: [a] -> [a] > myTail_pat (x:xs) = xs > myTail_pat [] = []
但是这个:
> myTail_guard (x:xs) | null xs = [] > | otherwise = xs
给我错误:程序错误:模式匹配失败:myTail_guard []如何声明没有模式的函数?
谢谢.
该模式x:xs
与空列表不匹配.你需要这样做:
myTail_guard xs | null xs = [] | otherwise = tail xs
1滴是安全的
drop 1 [] -- result: []