Skip to content

Instantly share code, notes, and snippets.

@hugoestr
Created March 22, 2016 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugoestr/f299fedf7d6994b3f1fe to your computer and use it in GitHub Desktop.
Save hugoestr/f299fedf7d6994b3f1fe to your computer and use it in GitHub Desktop.
defmodule Htest do
def equals(message, expect, value) do
IO.puts "#{message}: #{expect == value} "
end
end
defmodule Qsort do
def qsort([]), do: [] #base case for qsort
def qsort([pivot|[]]), do: [pivot] #base case for qsort
def qsort([pivot|tail]) do #how to get head and tail from
lower = Enum.filter(tail, fn(n) -> n < pivot end) #How to filter
higher = Enum.filter(tail, fn(n) -> n > pivot end)
qsort(lower) ++ [pivot] ++ qsort(higher)
end
end
# Test setup
list = [ 6, 1, 3, 5, 9, 8, 4, 7, 2]
result = Qsort.qsort list
expected = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# learned how to inspect in elixir
# IO.inspect result
IO.puts "testing"
Htest.equals "the list is sorted", expected, result
IO.inspect result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment