Written by Isaiah Oloyede
on
on
Chapter 3: Strings
This note captures my solutions to exercises in Chapter 3: Strings of the book Haskell programming from first principles
Exercises: Scope
Question 1
Prelude> let x = 5
Prelude> let y = 7
Prelude> let z = x * y
y is in scope for z.
Question 2
Prelude> let f = 3
Prelude> let g = 6 * f + h
h is not in scope for function g.
Question 3
area d = pi * (r * r)
r = d / 2
Everything we need to execute area is not in scope. r is not in scope in the area function. d is not in scope in the r function.
Question 4
area d = pi * (r * r)
where r = d / 2
r and d are now in scope for area.
Exercises: Syntax errors
++ [1, 2, 3] [4, 5, 6]should be(++) [1, 2, 3] [4, 5, 6]'<3' ++ ' Haskell'should be"<3" ++ " Haskell"concat ["<3", " Haskell"]compiles.
Concatenation and scoping
Chapter Exercises
Reading Syntax
concat [[1, 2, 3], [4, 5, 6]]is written correctly. The result is[1,2,3,4,5,6]++ [1,2,3] [4,5,6]is not written correctly. The++should be(++). The result of(++) [1,2,3] [4,5,6]is[1,2,3,4,5,6].(++) "hello" " world"is written correctly. The result is "hello world".["hello" ++ " world]is not written correctly." worldshould be" world". The result of["hello" ++ " world"]is["hello world"].4 !! "hello"is not written correctly.4 !! "hello"should be"hello" !! 4. The result of"hello" !! 4iso.(!!) "hello" 4is written correctly. The result of(!!) "hello" 4iso.take "4 lovely"is not written correctly.take "4 lovely"should betake 4 "lovely". The result oftake 4 "lovely"is"love".take 3 "awesome"is written correctly. The result oftake 3 "awesome"is"awe".
Result Matching
concat [[1 * 6],[2 * 6], [3 * 6]]gives[6, 12, 18](option d)."rain" ++ drop 2 "elbow"gives"rainbow"(option c).10 * head [1,2,3]gives10(option e).(take 3 "Julie") ++ (tail "yes")gives"Jules"(option a).concat [tail [1, 2, 3], tail [4, 5, 6], tail [7, 8, 9]]gives[2,3,5,6,8,9](option b).
Building functions
1
-- #1
-- a)
"Curry is awesome" ++ "!"
-- b)
take 1 $ drop 4 $ "Curry is awesome" ++ "!"
-- c)
drop 9 $ "Curry is awesome" ++ "!"