Chapter 3 Daily 07

Daily 7 (Not Quiz)

Agenda

  • Mention and demo: if-then and guards

  • Recursion Practice

  • Introduction to number bases: base 2, base 16

    • How do you write digits larger than 10?
    • Math practice

Making Choices

  1. Have the computer make a list of all of the perfect fourth powers between 1000 and 5000.

  2. Write a function q2 :: Int -> String that gives “Stone” when the input is 2, “Soup” when the input is 1, and otherwise gives “Hungry”.

  3. Modify your function so that instead of “Hungry” it returns the string version of the number. Examples:

     q2 10 == "10"
     q2 8 == "8"
    
  4. Write a function q4 :: Int -> Int so that q4 x so that an input of 3 gives 1000, an input of 4 gives 900, and any other input x gives an output of 900-10*x.

  5. Write a specific signature that works for the function q5 below. (No “generic typeclasses” like Num.)

     q5 a b c = (zip a [1..]) ++ [(b, sum [length d | d <- c])]
    

Daily 8 (Quiz)

Problem Solving

  1. Without using recursion, count how many numbers in a list are less than 5.

     below5 [1,3,5,7,9,11] == 2
    
  2. Using recursion but without an explicit if-then-else statement, count how many times the word “moon” appears in a list.

     countMoon ["cow","moon","a","b","moon"] == 2
    

    Can you figure out how to do this without “guards” too?

  3. Count how many times the consecutive digits 109 appear in a number.

     my109 :: Integer -> Integer
     my109 12310987109109 == 3