我正在尝试实现该功能tails.这是我的尝试:
tails' :: [a] -> [[a]]
tails' [] = []
tails' (x:xs) = xs:[[tails' xs]]
Run Code Online (Sandbox Code Playgroud)
我一直遇到编译错误:
Couldn't match expected type ‘a’ with actual type ‘[[a]]’
‘a’ is a rigid type variable bound by
the type signature for tails' :: [a] -> [[a]] at..
Run Code Online (Sandbox Code Playgroud)
我的实施有什么问题?
更换
tails' (x:xs) = xs:[[tails' xs]]
Run Code Online (Sandbox Code Playgroud)
有:
tails' (x:xs) = xs : tails' xs
Run Code Online (Sandbox Code Playgroud)
除了语法类型错误之外,您的实现不正确(符合规范)。对比一下这个...
Prelude> let tails [] = [[]]
Prelude| tails y@(x:xs) = y:(tails xs)
Prelude|
Prelude> tails "abc"
["abc","bc","c",""]
Run Code Online (Sandbox Code Playgroud)