Haskell CW II.3b
A few places use this variable:
word = "alphabet soup is tasty"
Try each question, ask neighbors and discuss. These took about 25 minutes of thinking and talking, with some help along the way.
-
Make the pattern
[1,4,7,10,13,16,19,22,25,28]
without so much typing. -
Make the pattern
['a','d','g','j','m']
without so much typing. Numbers and letters work in very similar ways. -
How do you take two lists of numbers and make a list of ordered pairs, like this:
a3 = [10,20,30,40,50,60,70] b3 = [13,27..80] -- [13,27,41,55,69] want3 = [( 10,13), (20,27), (30, 41) ] -- etc
-
What if you want to take the
word
from the start and make ordered pairs like this:[('a',10), ('l',20), ('p',30), ('h',40), ('a',50) ] -- etc
-
What do you do if you want the pattern to stop after the word “alphabet”? What if you don’t want it to stop, but keep going for every letter in the word?
-
How can you process
word
to only keep the letters that are'h'
or later in the alphabet? -
What does this do? (Without running.)
[ x>='h' | x <- word]
-
Learn what to put in the place of “…something…” to make
y=3*x-5
. (Attempt it; see a solution if needed.)[ y | x <- [0..5], ...something... ]
-
Figure out what to put in the place of
...something...
so that you get the same result as the previous exercise.[ y | x <- [0..5], y <- [...something...] ]
-
Use the function
y=3*x-5
. Make a list of ordered pairs(x,y)
that havex
an integer between 0 and 50 (inclusive) andy
less than 20. -
Annoying Detail I:
Int
is a 64 bit integer,Integer
has no size limit. Run the code below and compare the values:ap
andbp
. What do you see?a :: [Int] a = [1..100] b :: [Integer] b = [1..100] ap = product a bp = product b
-
Annoying Detail II: Try the same code, but change the upper limit from
100
to50
. Describe the difference you see.