dimanche 14 août 2016

Simple Haskell function that returns all functions defined in a file

I'm learning functional programming by learning Haskell. For practicing the syntax and structuring very simple functions, I've created a small file; let's say it looks something like this:

removeOdd nums =
  if null nums
    then []
    else
      if (mod (head nums) 2) == 0 --is even?
        then (head nums) : (removeOdd (tail nums))
        else removeOdd (tail nums)

removeOddGuards [] = []
removeOddGuards (x : xs)
  | mod x 2 == 0 = x : (removeOdd xs)
  | otherwise    = removeOdd xs

double nums = case nums of
  []       -> []
  (x : xs) -> (2 * x) : (double xs)

I would like to define a function which returns a list of all of the names of functions defined in the file, like:

["removeOdd", "removeOddGuards", "double"]

I suppose I would implement reflection in an object-oriented language, but am as of yet too naïve to understand if this is even a valid concept in functional programming.

For practical purposes, I'd like to be able to call such a function to see a basic index of the functions I've created in this practice file.





Aucun commentaire:

Enregistrer un commentaire