Reverse a list in haskell

后端 未结 5 796
野的像风
野的像风 2020-12-14 19:06

I am trying to reverse a list.

Following is my code:

reverseList :: [Int] -> [Int]
reverseList [] = []
reverseList (x:xs) =  x:reverseList xs
         


        
5条回答
  •  没有蜡笔的小新
    2020-12-14 20:03

    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:

    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].

提交回复
热议问题