部分功能组成的语法
module Luhn (isValid) where
import qualified Data.Char as C
isAsciiAlpha :: Char -> Bool
isAsciiAlpha = C.isAsciiLower || C.isAsciiUpper
isValid :: String -> Bool
isValid n
| any ((isAsciiAlpha || C.isSpace) . not) n = False
| otherwise = ys > 1 && sum xxs `mod` 10 == 0
where
xs = reverse [c | c <- n, isAsciiAlpha c]
ys = length xs
zs = zip xs (cycle [1, 2])
xxs = [convert x y | (x, y) <- zs]
convert :: Char -> Int -> Int
convert c mul =
do
let n = C.digitToInt c
case () of
_
| mul == 2 && (n > 4) -> n * mul - 9
| otherwise -> n * mul
我正在为这一行挣扎: any((isasciialpha || c.isspace)。没有)n = false
。我想要的很明显。找到除了ASCII字母或空间以外的任何字符。
尽管尝试了各种语法,但我仍在此行中遇到编译错误,类似
• Couldn't match expected type ‘Bool’
with actual type ‘Char -> Bool’
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法使用
(||):: bool - &gt;布尔 - &gt; bool
在两个功能上:参数应为bool
s。您可以做的是构造一个函数,该函数映射c
isasciialpha c || c.isspace c
,so\ c - &gt; isasciialpha c || c.isspace c
,或者您可以使用lifta2 ::应用f =&gt; (a - &gt; b - &gt; c) - &gt; fa-&gt; fb - &gt; f C
带有lifta2(||)isasciialpha c.isspace
。 也应应用于功能的结果,因此:您还可以使用
(&lt; ||&gt;)::应用a =&gt;布尔 - &gt;布尔 - &gt; bool
或它的shortcircuitng版本(||^)::::: monad m =&gt; m bool - &gt; m bool - &gt; m bool
软件包。You can not use
(||) :: Bool -> Bool -> Bool
on two functions: the parameters should be bothBool
s. What you can do is construct a function that maps a characterc
onisAsciiAlpha c || C.isSpace c
, so\c -> isAsciiAlpha c || C.isSpace c
, or you can useliftA2 :: Applicative f => (a -> b -> c) -> f a -> f b -> f c
withliftA2 (||) isAsciiAlpha C.isSpace
. Thenot :: Bool -> Bool
should also be applied on the result of the function, so:You can also make use of
(<||>) :: Applicative a => a Bool -> a Bool -> a Bool
or its shortcircuitng version(||^) :: Monad m => m Bool -> m Bool -> m Bool
of theprotolude
package.