Reverse.hs

-- | Purely functional data structures,
-- First example: reversing a list
-- Functional Programming course 2018.
-- Thomas Hallgren

{-
This started as a skeleton, the definitions were filled in
during the lecture.
-}
--------------------------------------------------------------------------------

import Prelude hiding (sum,reverse)

-- | Computing the sum of a list of numbers
sum :: [Int] -> Int
sum []     = 0
sum (x:xs) = x+sum xs

-- | Reversing a list
reverse_v1 :: [a] -> [a]
reverse_v1 []     = []
reverse_v1 (x:xs) = reverse_v1 xs ++ [x]


-- A more efficient way to reverse a list...
reverse :: [a] -> [a]
reverse xs = revOnto xs []

-- revOnto xs ys = reverse xs ++ ys
revOnto [] ys = ys
revOnto (x:xs) ys = revOnto xs (x:ys)

Plain-text version of Reverse.hs | Valid HTML?