首页 > 解决方案 > 在haskell中将一个元素从一个列表移动到另一个列表

问题描述

早上我在做一个练习,这是我第一次在 Haskell 练习是把第一个元素从我的第二个列表移动到我的第一个列表的第一个位置

结果:测试 [] [4,2,5,7] => ([4], [2,5,7])

That is my code :
test ::[a] -> [b] -> ([a],[b])
test [] [] = ([], [])
test [x] [] = ([x], [])
test [] [x] = ([x], [])
test [y] [x] = ([x:y], [])
But i got errors so please help me

这是我的错误

    • Couldn't match expected type ‘[b]’ with actual type ‘a’
      ‘a’ is a rigid type variable bound by
        the type signature for:
          pa :: forall a b. [a] -> [b] -> ([a], [b])
        at Pushswap.hs:30:6
    • In the second argument of ‘(:)’, namely ‘y’
      In the expression: x : y
      In the expression: [x : y]
    • Relevant bindings include
        x :: b (bound at Pushswap.hs:34:9)
        y :: a (bound at Pushswap.hs:34:5)
        pa :: [a] -> [b] -> ([a], [b]) (bound at Pushswap.hs:31:1)
Failed, modules loaded: none.

标签: haskell

解决方案


在实施中

test [y] [x] = ([x:y], [])

yis of type axis of typeb并且[x:y]需要是 type [a],因此错误。

通过使两个列表的类型相同,您应该能够克服此错误。


推荐阅读