Your Turn

  • Exercise: ListsAndRecursion-5

    Implement the following Enum functions using no library functions or list comprehensions: all?, each, filter, split, and take. You may need to use an if statement to implement filter. The syntax for this is

     
    if​ condition ​do
     
    expression(s)
     
    else
     
    expression(s)
     
    end
  • Exercise: ListsAndRecursion-6

    (Hard) Write a flatten(list) function that takes a list that may contain any number of sublists, which themselves may contain sublists, to any depth. It returns the elements of these lists as a flat list.

     
    iex>​ MyList.flatten([ 1, [ 2, 3, [4] ], 5, [[[6]]]])
     
    [1,2,3,4,5,6]

    Hint: You may have to use Enum.reverse to get your result in the correct order.

Get Programming Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.