我正在创建一个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> 发布于 2016-03-29 22:03:42
这就是你所需要的
getPairOfXAxesAndY :: (Num a, Ord a) => [(a,a)] -> [(a,a)]
getPairOfXAxesAndY [] = []
getPairOfXAxesAndY xs = fillPairs (0,0) xs
where fillPairs _ [] = []
fillPairs (a,b) ((c,d):ys) | a < c = (a,b) : fillPairs (a+1,b) ((c,d):ys)
| otherwise = (c,d) : fillPairs (c+1,d) yshttps://stackoverflow.com/questions/36295436
复制相似问题