【发布时间】:2019-10-09 14:20:15
【问题描述】:
如果输入字符串通过几个条件验证,我想测试它。当我需要将函数应用于列表的其余元素时,我陷入了困境。我应该如何处理这种情况?
我的代码如下:
import Data.Char
listLower = ['a'..'z']
listUpper = ['A'..'Z']
listNum = ['0'..'9']
listRes = ["if","then","else","module","import"]
isIdentifierStart :: Char -> Bool
isIdentifierStart x = x `elem` listLower
isIdentifierStart _ = False
isIdentifierPart :: Char -> Bool
isIdentifierPart x = x `elem` listLower || x `elem` listUpper || x `elem` listNum
isIdentifierPart _ = False
isReserved :: String -> Bool
isReserved x = x `elem` listRes
isReserved _ = False
isValid :: String -> Bool
isValid (x:xs) = (isIdentifierStart x) && (isIdentifierPart xs) && (not (isReserved [x]))
我得到的错误信息:
hf4.hs:22:61: error:
• Couldn't match expected type ‘Char’ with actual type ‘[Char]’
• In the first argument of ‘isIdentifierPart’, namely ‘xs’
In the first argument of ‘(&&)’, namely ‘(isIdentifierPart xs)’
In the second argument of ‘(&&)’, namely
‘(isIdentifierPart xs) && (not (isReserved [x]))’
|
22 | isValid (x:xs) = (isIdentifierStart x) && (isIdentifierPart xs) && (not (isReserved [x])) |
【问题讨论】:
-
使用
map,但在这里您可能想使用any或all。