gpt4 book ai didi

haskell - 是否存在类似 (xs :x)

转载 作者:行者123 更新时间:2023-12-03 15:09:43 24 4
gpt4 key购买 nike

我是 Haskell 的新手。我知道我可以创建一个 reverse通过这样做来发挥作用:

reverse :: [a] -> [a]
reverse [] = []
reverse (x:xs) = (Main.reverse xs) ++ [x]

有没有 (xs:x)这样的东西(与一个元素连接的列表,即 x 是列表中的最后一个元素)以便我将最后一个列表元素放在列表的前面?
rotate :: [a] -> [a]
rotate [] = []
rotate (xs:x) = [x] ++ xs

当我尝试编译包含此函数的程序时出现这些错误:
Occurs check: cannot construct the infinite type: a = [a]
When generalising the type(s) for `rotate'

最佳答案

我也是 Haskell 的新手,所以我的回答并不权威。无论如何,我会使用 lastinit :

Prelude> last [1..10] : init [1..10]
[10,1,2,3,4,5,6,7,8,9]

或者
Prelude> [ last [1..10] ] ++ init [1..10]
[10,1,2,3,4,5,6,7,8,9]

关于haskell - 是否存在类似 (xs :x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5804113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com