User Tools

Site Tools


cs326:lab2

CSci 326 - Lab #2

  1. Work through the Enum exercise at ElixirSchool. There is a typo in the first “experiment”, so try this:
    Enum.__info__(:functions) |> Enum.each(fn({function, arity}) -> IO.puts "#{function}/#{arity}" end)
  2. All the examples use lists, but the Enum module works for other enumerables, like map. Try this one out:
    Enum.each(%{1 => "one", 2 => "two", 3 =>"three"}, fn({key, value}) -> IO.puts(key) end)
  3. Now try this one:
    Enum.each(%{1 => "one", 2 => "two", 3 =>"three"}, fn({key, value}) -> IO.puts(value) end)
  4. So to enumerate over maps, we need an anonymous function that takes a {key, value} tuple. Come up with an example of your own for both Enum.map and Enum.reduce that work on map collections. Show your results to the instructor.
  5. Try this and figure out the value bound to cost:
    cost = "$34.56" |> String.split("$") |> tl |> List.first |> String.to_float
  6. Now work through the Comprehensions exercise at ElixirSchool.

Have a look at the docs for the String, List, and Enum modules while trying the homework problems.

cs326/lab2.txt · Last modified: 2018/02/19 11:47 by scarl