28 Jun 2013, 00:16
Photo may 19, 5 22 08_pragsmall

James Martin (1 post)

My solution to the @flatten(list)@ exercise in section 7.6 doesn’t use reverse, as the hint suggests. Here’s my version:

def flatten([]), do: []
def flatten([head|tail]), do: flatten(head) ++ flatten(tail)
def flatten(head), do: [head]

I’m curious what the solution looks like if you do use reverse to solve this problem. Anybody care to share their solution?

28 Jun 2013, 18:45

Dave Thomas (338 posts)

In general, you probably want to avoid ++. It is considerably less efficient that building a list using [ ]
  You must be logged in to comment