Skip to content

Latest commit

 

History

History
182 lines (134 loc) · 4.38 KB

math_with_guards.livemd

File metadata and controls

182 lines (134 loc) · 4.38 KB

Math With Guards

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Math

In this exercise, you're going to use guards to create a Math module that handles adding and subtracting integers, strings, and lists. You do not need to handling adding or subtracting different data types.

Math.add(1, 2)
3

Math.add("Hello, ", "World!")
"Hello, World!"

Math.add([1, 2], [3, 4])
[1, 2, 3, 4]

Math.subtract(10, 2)
8

Math.subtract("hello there", "hello ")
"there"

Math.subtract([1, 2, 3, 4], [1, 3])
[2, 4]
Hint: Subtracting Strings

Consider converting your strings to a list, then subtract the two lists together then join your result back into a string.

Example Solution
defmodule Math do
  def add(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
    integer1 + integer2
  end

  def add(list1, list2) when is_list(list1) and is_list(list2) do
    list1 ++ list2
  end

  def add(string1, string2) when is_binary(string1) and is_binary(string2) do
    string1 <> string2
  end

  def subtract(integer1, integer2) when is_integer(integer1) and is_integer(integer2) do
    integer1 - integer2
  end

  def subtract(list1, list2) when is_list(list1) and is_list(list2) do
    list1 -- list2
  end

  def subtract(string1, string2) when is_binary(string1) and is_binary(string2) do
    String.split(string1, "")
    |> subtract(String.split(string2, ""))
    |> Enum.join()
  end
end

Implement the Math module as documented below.

defmodule Math do
  @moduledoc """
  Documentation for `Math`
  """

  @doc """
  Add two integers, strings, or lists.

  ## Examples

    iex> Math.add(2, 2)
    4
    iex> Math.add(4, 4)
    8

    Math.add([1, 2], [3, 4])
    [1, 2, 3, 4]
    Math.add([1, 2, 3], [4, 5, 6])
    [1, 2, 3, 4, 5, 6]

    iex> Math.add("abc", "def")
    "abcdef"
    iex> Math.add("123", "456")
    "123456"

    iex> Math.add(1.0, 2.0)
    ** (FunctionClauseError) no function clause matching in Math.add/2 

    iex> Math.add(2, "2")
    ** (FunctionClauseError) no function clause matching in Math.add/2 

    iex> Math.add({}, {})
    ** (FunctionClauseError) no function clause matching in Math.add/2 

    iex> Math.add(%{}, %{})
    ** (FunctionClauseError) no function clause matching in Math.add/2 
  """
  def add(value1, value2) do
  end

  @doc """
  Subtract two integers, strings, or lists.

  ## Examples

    iex> Math.subtract(10, 2)
    8
    iex> Math.subtract(20, 2)
    18

    iex> Math.subtract([1, 2], [1])
    [2]
    iex> Math.subtract([1, 2, 3, 4], [1, 3])
    [2, 4]

    iex> Math.subtract("abcd", "abc")
    "d"
    iex> Math.subtract("cbad", "abc")
    "d"
    iex> Math.subtract("abc", "xyz")
    "abc"
    iex> Math.subtract("abc", "xyza")
    "bc"

    iex> Math.subtract(1.0, 2.0)
    ** (FunctionClauseError) no function clause matching in Math.subtract/2 

    iex> Math.subtract(2, "2")
    ** (FunctionClauseError) no function clause matching in Math.subtract/2 

    iex> Math.subtract({}, {})
    ** (FunctionClauseError) no function clause matching in Math.subtract/2 

    iex> Math.subtract(%{}, %{})
    ** (FunctionClauseError) no function clause matching in Math.subtract/2 
  """
  def subtract(value1, value2) do
  end
end

Commit Your Progress

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish math with guards exercise"

Up Next

Previous Next
Message Validation RPS Guards