Lab 4: Comprehending Lists
CIS 252
i
Introduction to Computer Science
You may work singly or in pairs on this lab:
If you work with a partner, turn
in a
single
solution with
both names on it.
1. Lists
In Haskell,
a list
is a
sequence of values,
all having
the same
type.
For example,
[1,3,5,6]
is a 4element list of
Int
s, whereas
[True,False,False,True,False]
is a 5element list of
Bool
s.
Generally speaking, the lists that we’ll be using will be ﬁnite (in particular, all of
the lists we use in this lab will be ﬁnite, with one exception). However, Haskell
does allow us to deﬁne and use
inﬁnite lists; we’ll talk about the use of inﬁnite
lists later this semester.
In Haskell, strings are lists of characters, just as strings in C are just arrays of
characters. If you evaluate the expression
[’a’, ’b’, ’c’]
in the Ghci inter
preter, you’ll get the following response:
"abc"
.
Haskell provides mechanisms for the easy deﬁnition of certain lists. Evaluate
each of the following at the Ghci prompt:
[1 .
. 10]
[15 .
. 20]
[15 .
. (205)]
[’q’ .
. ’z’]
[14 .
. 2]
You no doubt noticed the pattern here: for numbers, characters and other enu
merated types (something else we haven’t talked about yet!), the expression
[
m
..
n
]
yields the list
[
m
,
m
+
1
,
m
+
2
, ...,
n
]
. In the cases where
n
is less than
m
(such as the last example above), the empty list
[]
is returned.
More generally, the expression
[
m
,
p
..
n
]
yields the list
[
m
,
m
+ (
p

m
)
,
m
+
2
(
p

m
)
,
m
+
3
(
p

m
)
, ...,
n
0
]
where
n
0
is as close as possible to
n
without exceeding it. The book explains this