Haskell CW II.4
Please write type signatures for all of your functions.
Introduction
demo n xs = [ n*x | x <- xs]
-
Write an example of how you would call
demo
. -
What is the type signature of
demo
assuming we make all numbersInt
. -
Add an exception so that
demo 0 anything
always returns the empty list. -
We are going to learn about the
(x:xs)
pattern that we use to write recursive functions, likefirst
andrest
from Racket. Use that method to write a recursive version of thedemo
function. -
Check for understanding: write
mylength
, a recursive function that finds the length of a list of numbers.
Exercises
-
Write a function
f
that takes in an Int and always returns 5. -
Change
f
so that when the input is 0 it returns 8, and when the input is 1 it returns 13. -
Change
f
more so that it makes a Fibonacci sequence beginning with the 8 and 13 that you already have. $$f(n) = f(n-1) + f(n-2).$$ -
Write a function
sub5
that takes in a list of integers and puts out a list of integers five less than the inputs. -
Write a function
skip13
that takes in a list of integers and puts out a list of integers that is the same except excluding any 13s. -
(
sumByHand
): Write a recursive function that adds all of the numbers in a list. -
Write a function
before14
that takes in a list of integers and inserts a 13 before every 14 in the list.