创建haskell函数,该函数接收对列表并使用新数据和现有数据创建新对

时间:2016-03-29 21:29:14

标签: haskell

我正在创建一个执行下一个功能的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> 

1 个答案:

答案 0 :(得分:1)

这就是你需要的

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) ys