O'Reilly logo

Haskell Cookbook by Yogesh Sajanikar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Change directory to reverse, and open src/Main.hs. This file defines the Main module.
  2. Conditionally import Prelude, hiding the reverse function:
        import Prelude hiding (reverse)
Note that Prelude is imported automatically unless it is explicitly imported. Since we will write our own reverse function, we need to hide the reverse function while importing Prelude
  1. Write the signature of a reverse function:
        reverse :: [a] -> [a]

This function takes a list of any type, a, and returns the reversed list.

  1. Implement the reverse function using recursion:
        reverse :: [a] -> [a]        reverse xs = reverse' xs []        where          reverse' :: [a] -> [a] -> [a]          reverse' [] rs = rs          reverse' (x:xs) rs = reverse' xs (x:rs)

Note that we have a written ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required