February 2018
Beginner
200 pages
4h 37m
English
Here’s the code of how you can find the smallest and biggest numbers in a list:
| | defmodule MyList do |
| | def max([]), do: nil |
| | def max([a]), do: a |
| | def max([a, b | rest]) when a >= b, do: find_max(rest, a) |
| | def max([a, b | rest]) when a < b, do: find_max(rest, b) |
| | |
| | defp find_max([], max), do: max |
| | defp find_max([head | rest], max) when head >= max, do: find_max(rest, head) |
| | defp find_max([head | rest], max) when head < max, do: find_max(rest, max) |
| | |
| | def min([]), do: nil |
| | def min([a]), do: a |
| | def min([a, b | rest]) when a <= b, do: find_min(rest, a) |
| | def min([a, b | rest]) when a > b, do: find_min(rest, ... |
Read now
Unlock full access