我试图遍历列表的递归并检查是否所有值都等于0,即时消息错误是:
* Couldn't match expected type `[Integer]' with actual type `Bool'
* In the second argument of `(:)', namely `allZero s'
In the expression: 0 : allZero s
In an equation for `allZero': allZero (0 : s) = 0 : allZero s
Run Code Online (Sandbox Code Playgroud)
我的代码是:
allZero :: [Int] -> Bool
allZero (0:_) = True
allZero (0:s) = 0 : allZero s
allZero (_:s) = False;
allZero _ = False
Run Code Online (Sandbox Code Playgroud)
我不明白为什么我会收到此错误,因为allZero (0:s) = 0 : allZero s我给了它正确的参数,即列表“ s”
我正在尝试使用元组的第二个数字创建一个新列表,如果第一个数字在第一个列表中,这里是一个示例 helplist [0,2] [(0,"a"),(3,"x"),(5,"y"),(1,"b"),(2,"c")] = ["a","c"]
我尝试了很多组合,但是没有成功,这是我的代码:
helplist :: [Int] -> [(Int,Char)] -> [Char]
helplist (x:xs) ((i,g):gs) | (x == i) = g : helplist xs gs
helplist _ _ = []
Run Code Online (Sandbox Code Playgroud)
在Java中,我做了2次for循环,但是在haskell中,我只知道递归。
我在使用liftM. 因为(+)它工作正常,该函数
madd a b = liftM2 (+) a b给了我预期的结果,Just 5 `madd` Just 7 = Just 12
但现在尝试(/)它给了我奇怪的结果。
mdiv a b = liftM2 (/) a b现在手术Just 12 `mdiv` Just 0给了我,Just Infinity
而我期待Nothing。
如何增加属性?我创建了一个用户类型
data user = User{
username :: String,
passwort :: String,
points :: Int
}
user = user {username ="Test",
passwort="test123",
points=100
}
Run Code Online (Sandbox Code Playgroud)
我想将点数增加10,所以我尝试做类似其他编程语言的操作 points += 10
userplus10 = user{points = points +10}
Run Code Online (Sandbox Code Playgroud)
(顺便说一句,这不起作用)创建一个新用户,但在点上具有另一个值。