Skip to content

Instantly share code, notes, and snippets.

@brandonpittman
Created September 20, 2017 00:20
Show Gist options
  • Save brandonpittman/301bdf5c5f84dfe517a2bdf72fb1907c to your computer and use it in GitHub Desktop.
Save brandonpittman/301bdf5c5f84dfe517a2bdf72fb1907c to your computer and use it in GitHub Desktop.
defmodule Lists do
def len([]), do: 0
def len([ _head | tail ]), do: 1 + len(tail)
def sum([]), do: 0
def sum([h | t]), do: h + sum(t)
def double([]), do: []
def double([head | tail ]), do: [2*head | double(tail)]
def square([]), do: []
def square([head | tail ]), do: [head*head | square(tail)]
def sum_pairs([]), do: []
def sum_pairs([ h1, h2 | t]), do: [ h1 + h2 | sum_pairs(t) ]
def even_length?([]), do: true
def even_length?([ _, _ | [_]]), do: false
def even_length?([ _, _ | _]), do: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment