use*_*425 4 haskell pattern-matching
我是Haskell的新手,我正在尝试编写一个带有列表并返回bool的函数.
True如果其输入列表是'a'仅包含的列表,则返回,False否则返回.
这是我最好的猜测:
f :: [a] -> Bool
f ('a':[]) = True
f (x:xs) = False
Run Code Online (Sandbox Code Playgroud)
这无法编译并返回:
Couldn't match type `a' with `Char'
`a' is a rigid type variable bound by
the type signature for f :: [a] -> Bool at charf.hs:6:1
In the pattern: 'b'
In the pattern: 'b' : []
In an equation for `f': f ('b' : []) = True
Run Code Online (Sandbox Code Playgroud)
我的逻辑错误是什么?
f :: [Char] -> Bool
f ['a'] = True
f _ = False
Run Code Online (Sandbox Code Playgroud)
使用模式匹配.您的函数似乎没有处理空列表.此外,您的功能不能像您想要的那样通用,因为它显然需要[Char](或a String).
| 归档时间: |
|
| 查看次数: |
1728 次 |
| 最近记录: |