Control flow exercises

A few completely optional exercises about control flow to try.


๐Ÿ““Exercise: Rearrange pattern match cases

The following pattern match is in disarray. The ordering of the case statements doesn't allow some cases to be reached. Reorder the cases so that the evaluation of the pattern match produces the desiredtrueresult for the boolean expression.

use Universal
itDependsUpon : Optional Text -> Text
itDependsUpon = cases
  _ -> "So much depends"
  Some textValue -> "upon"
  None -> "a red wheel barrow"
  Some x | size x === 3 -> "glazed with rain"
  optionalValue | Optional.contains "all" optionalValue -> "water"

(itDependsUpon None === "a red wheel barrow") && (itDependsUpon (Some "spring") === "upon") && (itDependsUpon (Some "and") === "glazed with rain") && (itDependsUpon (Some "all") === "water")
๐Ÿ”‘Answer:
๐Ÿ”‘Answer:

One possible answer:

itDependsUpon : Optional Text -> Text itDependsUpon = cases optionalValue | Optional.contains "all" optionalValue -> "water" Some x | Text.size x === 3 -> "glazed with rain" Some textValue -> "upon" None -> "a red wheel barrow" _ -> "So much depends" (itDependsUpon None === "a red wheel barrow") && (itDependsUpon (Some "spring") === "upon") && (itDependsUpon (Some "and") === "glazed with rain") && (itDependsUpon (Some "all") === "water")
โงจ
true

Note, you could also have a working answer with theNonecase at the top of the match statement.


๐Ÿ““Exercise: Practice using casessyntax

Rewrite the following function to usecasesinstead ofmatch โ€ฆ with:

authorMatcher : Text -> Optional Text
authorMatcher book = match book with
  "Of Mice and Men" -> Some "John Steinbeck"
  "To Kill a Mockingbird" -> Some "Harper Lee"
  "The Tempest" -> Some "William Shakespeare"
  _ -> None
๐Ÿ”‘Answer
๐Ÿ”‘Answer

The argument to the function can be inferred, remove the reference to it in the term definition and substitutecasesfor thematch book withphrase.

authorMatcher : Text -> Optional Text
authorMatcher = cases
  "Of Mice and Men" -> Some "John Steinbeck"
  "To Kill a Mockingbird" -> Some "Harper Lee"
  "The Tempest" -> Some "William Shakespeare"
  _ -> None

Rewrite the following multi-argument function with thecasessyntax.

use Nat
  matchTwo : Nat -> Nat -> Text
  matchTwo n1 n2 = match (n1,n2) with
    (a, b) | a < 3 -> "small number first"
    (a, b) | b < 3 -> "small number second"
    _ -> "big number"

  matchTwo 5 2
๐Ÿ”‘Answer
๐Ÿ”‘Answer
matchTwo : Nat -> Nat -> Text
matchTwo = cases
  a, b 
    | a Nat.< 3 -> "small number first"
    | b Nat.< 3 -> "small number second"
  _, _ -> "big number"

๐Ÿ““Exercise: Rewrite an if/else expression as a pattern match

This if / else expression can also be written as a case statement. Translate it so that it uses amatchexpression.

ex4.myText : Text -> Text
ex4.myText word =
  if word === "Testing123" then "I'm a test"
  else
    use Nat >
    use Text size
    if size word > 8 then Text.repeat (size word) "!"
    else
      if (size word > 5) && (Text.take 1 word === "A") then
        "Starts with A"
      else
        if size word > 5 then "Long word"
        else if word === Text.reverse word then "Mirror" else "Other"

The following are a few test cases which should pass for your match statement version of the function.

ex4.test1 : [Test.Result]
ex4.test1 = check (ex4.myText "Testing123" === "I'm a test")
ex4.test2 : [Test.Result]
ex4.test2 = check (ex4.myText "aaaaahh!" === "Starts with A")
test3 : [Test.Result]
test3 = check (ex4.myText "Boooooooooo" === "!!!!!!!!!!!")
test4 : [Test.Result]
test4 = check (ex4.myText "hannah" === "Long word")
test5 : [Test.Result]
test5 = check (ex4.myText "wow" === "Mirror")
test6 : [Test.Result]
test6 = check (ex4.myText "Nope" === "Other")

๐Ÿ”‘Answer
๐Ÿ”‘Answer
answer.myText : Text -> Text
answer.myText = cases
  "Testing123" -> "I'm a test"
  t            
    | Text.size t Nat.> 8                              ->
      Text.repeat (Text.size t) "!"
    | (Text.size t Nat.> 5) && (Text.take 1 t === "a") ->
      "Starts with A"
    | Text.size t Nat.> 5                              -> "Long word"
    | t === Text.reverse t                             -> "Mirror"
  _            -> "Other"