作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 Haskell 新手,我正在尝试编写一个接受列表并返回 bool 的函数。
如果输入列表是仅由'a'
组成的列表,则返回True
,否则返回False
。
这是我最好的猜测:
f :: [a] -> Bool
f ('a':[]) = True
f (x:xs) = False
编译失败并返回:
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
我的逻辑有什么错误?
最佳答案
f :: [Char] -> Bool
f ['a'] = True
f _ = False
使用模式匹配。您的函数似乎无法处理空列表。此外,您的函数不能像您想要的那样通用,因为它显然需要一个 [Char]
(或 String
)。
关于haskell - 在 Haskell 中定义一个函数,如果它是由 'a' 组成的列表,则返回 true,否则返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18164772/
我是一名优秀的程序员,十分优秀!