早上我在做一个练习,这是我第一次在Haskell练习是将第二个列表中的第一个元素移动到第一个列表
的第一个位置Result: test [] [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.
在实现
test [y] [x] = ([x:y], [])
y
为a
类型,x
为b
类型,[x:y]
需要为[a]
类型,因此出现错误。
您应该能够通过使两个列表的类型相同来解决这个错误。