我开始使用the University of Pennsylvania's free online course materials来学习Haskell。这些都是故意上网的,所以我想通过问这个问题我没有帮助任何人的功课。模式匹配Haskell列表
我从下面的函数中得到一些编译器错误,我用它来回答第一个任务的一部分,但我找不出原因。我的作用是:
doubleEveryOther :: [Integer] -> [Integer]
doubleEveryOther [] = []
doubleEveryOther [x] = [x]
doubleEveryOther [x:y] = [x:(y*2)]
doubleEveryOther [x:y:ys] = [x:y*2:doubleEveryOther ys]
我得到的是这些错误:
01.hs:18:19:
Couldn't match expected type ‘Integer’ with actual type ‘[a0]’
In the pattern: x : y
In the pattern: [x : y]
In an equation for ‘doubleEveryOther’:
doubleEveryOther [x : y] = [x : (y * 2)]
01.hs:18:27:
Couldn't match expected type ‘Integer’ with actual type ‘[a0]’
Relevant bindings include
y :: [a0] (bound at 01.hs:18:21)
x :: a0 (bound at 01.hs:18:19)
In the expression: x : (y * 2)
In the expression: [x : (y * 2)]
In an equation for ‘doubleEveryOther’:
doubleEveryOther [x : y] = [x : (y * 2)]
01.hs:19:19:
Couldn't match expected type ‘Integer’ with actual type ‘[Integer]’
In the pattern: x : y : ys
In the pattern: [x : y : ys]
In an equation for ‘doubleEveryOther’:
doubleEveryOther [x : y : ys] = [x : y * 2 : doubleEveryOther ys]
01.hs:19:30:
Couldn't match expected type ‘Integer’ with actual type ‘[Integer]’
In the expression: x : y * 2 : doubleEveryOther ys
In the expression: [x : y * 2 : doubleEveryOther ys]
In an equation for ‘doubleEveryOther’:
doubleEveryOther [x : y : ys] = [x : y * 2 : doubleEveryOther ys]
谁能帮助我了解为什么我的模式不匹配正确的类型?
过了一个完整的答案累了,对不起,但下面可能会给你一个想法:'[X,Y]','(X:Y :YS)'。 – Zeta 2014-12-10 22:01:02