haskell - 在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.
解决方案
在实施中
test [y] [x] = ([x:y], [])
y
is of type a
,x
is of typeb
并且[x:y]
需要是 type [a]
,因此错误。
通过使两个列表的类型相同,您应该能够克服此错误。
推荐阅读
- javascript - Web 推送通知 - 收到通知但没有弹出窗口
- angular - 发生未处理的异常:项目不存在。角 SSR
- json - 重新排列 JSON 数据以显示在表格行中
- javascript - 使用传单嵌入地图中的音频 - 无法让它们工作
- javascript - 使用插图旋转幻灯片时,幻灯片中的图像会扭曲
- php - 如何在 Wordpress 中拥有同一网站的 2 个颜色版本?
- swift - 如何从 Firebase 实时数据库加载大量数据
- python - 如何播放/暂停 numpy 声音阵列?
- javascript - 两个看起来完全一样的数组怎么可能不同?
- go - 传递给 fmt.Sprintf 的字符串用方括号括起来