- These lines of code are from a REPL session. Is y in scope for z?
> x = 5
> y = 7
> z = x * y
Yes
- These lines of code are from a REPL session. Is h in scope for g?
> f = 3
> g = 6 \* f + h
No h does not seem to be declared previously.
- This code sample is from a source file. Is everything we need to execute area in scope?
area d = pi \* (r \* r)
r = d / 2
No Since d is local to the function area it can be accessed in the expression following r.
- This code is also from a source file. Now are r and d in scope for area?
area d = pi \* (r \* r)
where r = d / 2
Yes Since r is now defined in the local scope of area, it can access d which is defined in its wrapping scope.
-
For the following lines of code, read the syntax carefully and decide if they are written correctly. a.
concat [[1,2,3], [4,5,6]]
correct syntax.concat
takes a list of lists and returns one list of type equal to the nested lists after concatenating them together. b.++ [1, 2, 3] [4, 5, 6]
wrong syntax.++
is a infix operator. In order to use it prefix, it must be wrapped in parenthesis as follows:(++) [1, 2, 3] [4, 5, 6]
c.(++) "hello" " world"
correct syntax.++
takes two parameters and wrapped in parenthesis can be used as a prefix operator. d.["hello" ++ " world]
wrong syntax.world
is not correctly wrapped in double quotes. e.4 !! "hello"
wrong syntax. (!!) is of type[a] -> Int -> a
which means its first parameter is a list and the second parameter is an integer. The correct expression would be:"hello" !! 4
f.(!!) "hello" 4
correct syntax. (!!) takes a list and an Integer and the parenthesis makes it prefix. g.take "4 lovely"
wrong syntax.take
has the typeInt -> [a] -> [a]
but we are passing only a string,[Char]
, to it. The correct syntax is a follows:take 4 "lovely"
h.take 3 "awesome"
correct syntax. -
We have two sets: The first set is lines of code while the other set is results. Read the code and decide which result belongs to which set. a.
concat [[1 * 6], [2 * 6], [3 * 6]]
=>[6, 12, 18]
b."rain" ++ drop 2 "elbow"
=>"rainbow"
c.10 * head [1, 2, 3]
=>10
d.(take 3 "Julie") ++ (tail "yes")
=>Jules
e.concat [tail [1, 2, 3], tail [4, 5, 6], tail [7, 8, 9]]
=>[2, 3, 5, 6, 8, 9]
-
Given the list-manipulation functions mentioned in this chapter, write functions that take the following inputs and return the expected outputs. a. Given "Curry is awesome", return "Curry is awesome!"
concat ["Curry is awesome", "!"]
or"Curry is awesome" ++ "!"
b. Given "Curry is awesome!", return "y".drop 4 (take 5 "Curry is awesome!")
or"Curry is awesome!" !! 4
c. Given "Curry is awesome!", return "awesome!".drop 9 "Curry is awesome!"
-
Now, take each of the above and rewrite it in a source file as a general function that could take different string inputs as arguments but retain the same behavior. Solution file
-
Write a function of type
String -> Char
which returns the third character in aString
. Solution file -
Now change that function so the string operated on is always the same and the variable represents the number of the letter you want to return. Solution file
-
Using the
take
anddrop
functions write a function calledrvrs
that takes the string "Curry is awesome" and returns the string "awesome is Curry". Solution file -
Add the
rvrs
function to a module namedReverse
Solution file