【发布时间】:2016-07-17 15:58:51
【问题描述】:
我正在创建一个 haskell 函数来执行下一步:
给定 [(3,5),(6,9),(8,0)] 返回 [(0,0),(1,0),(2,0),(3,5),(4,5),(5,5),(6,9),(7,9)( 8,0)]
因此,一个元组列表,其键从 0 到列表参数第一个对值的最大值,对的第二个值将是 0,直到参数列表的第一个元素不作为第二个列表的当前元素的键出现,从那一刻,保留用于第二对元素的最后一个值,直到匹配下一个。
我希望它得到很好的解释:
这是我的功能
getPairOfXAxesAndY :: [(a,a)] -> [(a,a)]
getPairOfXAxesAndY [] = []
getPairOfXAxesAndY list = getSizes list 0
where getSizes((x, h):[]) d = (x, h)
getSizes((x, h):rl) d = (x, maybe d (+0) (lookup x ((x, h):rl))) : getSizes rl (maybe d lookup x)
但是我得到了这个错误:
Skyline.hs:39:22:
Couldn't match expected type ‘[(a, a)]’
with actual type ‘(Maybe a0, [(a0, b0)] -> Maybe b0)’
Relevant bindings include
list :: [(a, a)] (bound at Skyline.hs:39:15)
dibujaSkyline :: [(a, a)] -> [(a, a)] (bound at Skyline.hs:38:1)
In the expression: getAlturas list 0
In an equation for ‘dibujaSkyline’:
dibujaSkyline list
= getAlturas list 0
where
getAlturas ((x, h) : []) d = (x, h)
getAlturas ((x, h) : rl) d
= (x, maybe d (+ 0) (lookup x ((x, h) : rl)))
: getAlturas rl (maybe d lookup x)
Skyline.hs:41:37:
Couldn't match expected type ‘(Maybe a1, [(a1, b)] -> Maybe b)’
with actual type ‘[(Maybe a1, [(a1, b)] -> Maybe b)]’
Relevant bindings include
d :: [(a1, b)] -> Maybe b (bound at Skyline.hs:41:33)
rl :: [(Maybe a1, [(a1, b)] -> Maybe b)]
(bound at Skyline.hs:41:29)
h :: [(a1, b)] -> Maybe b (bound at Skyline.hs:41:26)
x :: Maybe a1 (bound at Skyline.hs:41:23)
getAlturas :: [(Maybe a1, [(a1, b)] -> Maybe b)]
-> ([(a1, b)] -> Maybe b) -> (Maybe a1, [(a1, b)] -> Maybe b)
(bound at Skyline.hs:40:11)
In the expression:
(x, maybe d (+ 0) (lookup x ((x, h) : rl)))
: getAlturas rl (maybe d lookup x)
In an equation for ‘getAlturas’:
getAlturas ((x, h) : rl) d
= (x, maybe d (+ 0) (lookup x ((x, h) : rl)))
: getAlturas rl (maybe d lookup x)
In an equation for ‘dibujaSkyline’:
dibujaSkyline list
= getAlturas list 0
where
getAlturas ((x, h) : []) d = (x, h)
getAlturas ((x, h) : rl) d
= (x, maybe d (+ 0) (lookup x ((x, h) : rl)))
: getAlturas rl (maybe d lookup x)
Skyline.hs:41:80:
Couldn't match expected type ‘[(Maybe a1, [(a1, b)] -> Maybe b)]’
with actual type ‘(Maybe a1, [(a1, b)] -> Maybe b)’
Relevant bindings include
d :: [(a1, b)] -> Maybe b (bound at Skyline.hs:41:33)
rl :: [(Maybe a1, [(a1, b)] -> Maybe b)]
(bound at Skyline.hs:41:29)
h :: [(a1, b)] -> Maybe b (bound at Skyline.hs:41:26)
x :: Maybe a1 (bound at Skyline.hs:41:23)
getAlturas :: [(Maybe a1, [(a1, b)] -> Maybe b)]
-> ([(a1, b)] -> Maybe b) -> (Maybe a1, [(a1, b)] -> Maybe b)
(bound at Skyline.hs:40:11)
In the second argument of ‘(:)’, namely
‘getAlturas rl (maybe d lookup x)’
In the expression:
(x, maybe d (+ 0) (lookup x ((x, h) : rl)))
: getAlturas rl (maybe d lookup x)
Failed, modules loaded: none.
Prelude>
【问题讨论】:
-
这是由递归调用中的
maybe d lookup x引起的。我不会做两次lookup并用maybe修复它...为什么不只做一个模式匹配?
标签: haskell