# lab4 - Lab 4 Comprehending Lists You may work singly or in...

This preview shows pages 1–2. Sign up to view the full content.

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 4-element list of Int s, whereas [True,False,False,True,False] is a 5-element 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 . . (20-5)] [’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

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 2

lab4 - Lab 4 Comprehending Lists You may work singly or in...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online