【发布时间】:2014-12-10 21:55:37
【问题描述】:
我开始使用the University of Pennsylvania's free online course materials 学习 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)。