反转haskell中的列表 [英] Reverse a list in haskell
问题描述
我正在尝试反转列表.
以下是我的代码:
reverseList :: [Int] -> [Int]
reverseList [] = []
reverseList (x:xs) = x:reverseList xs
最终发生的事情是我最终以相同的顺序取回列表.我什至有一个如何反转列表的解决方案,但我试图了解我在这里做错了什么?我对 haskell 很陌生,所以我认为我应该专注于理解更多,然后才能轻松解决更多问题.我知道这个问题有很多解决方案,但我需要更多帮助来理解我在这段代码中做错了什么.
What ends up happening is I end up getting the list back in same order. I even have a solution for how to reverse the list but I am trying to understand what I did wrong here ? I am very new to haskell so think I should focus on understanding more then I can solve more problems with ease. I know there are many solutions out there for this problem but I need more help in the understanding esp what I did wrong here in this code.
推荐答案
您将列表分成头部和尾部,然后以相同的顺序重新组合列表.以列表[1, 2, 3]
为例:
You are separating the list into head and tail, but then re-assemble the list in the same order. Take the list [1, 2, 3]
for example:
在第一次调用中,x
将是 1
,而 xs
将是 [2, 3]
.然后创建一个新列表,由前面的 x
(so 1) 和 reverseList [2, 3]
组成.
In the first call, x
will be 1
, and xs
will be [2, 3]
. Then you create a new list, consisting of x
(so 1) at the front, followed by reverseList [2, 3]
.
这篇关于反转haskell中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!