Summary: We continue our exploration of the Haskell programming language.

Prerequisites: The first Haskell lab. Tate, Section 8.3.

Contents:

## Preparation

a. Create a directory for the lab.

b. Open a browser window on Tate's examples, in case you want to try any of them.

## Exercises

### Exercise 1: Characters to Numbers

Write or find a function that converts digit characters to the corresponding number. (If you can't find one, you should find the `fromEnum` function useful.)

```Prelude> digitToInt '5'
5
```

### Exercise 2: Strings to Numbers

Write (do not find) a function that converts strings that represent integers to the corresponding integer. For example,

```Prelude> atoi "123"
123
Prelude> atoi "123" + 5
128
Prelude> "123" + 5
<interactive>:1:0:
No instance for (Num [Char])
arising from a use of `+' at <interactive>:1:0-8
Possible fix: add an instance declaration for (Num [Char])
In the expression: "123" + 5
In the definition of `it': it = "123" + 5
```

### Exercise 3: Strings to Numbers, Revisited

This exercise is based on a problem from Tate.

As you no doubt have noted, we often write larger numbers with commas to separate the portions. E.g., `3,123,876`. Revise your `atoi` function from the previous step to handle situations like this.

### Exercise 4: Selecting Elements

a. Write a function, `everyOther`, that takes a list as input and returns a list in which you've selected every other element of the input list. E.g.,

```Prelude> everyOther [1,2,3,4]
[1,3]
```

b. Write a function, `everyThird`, that takes a list as input and returns a list in which you've selected every third element of the input list. E.g.,

```Prelude> everyThird [1,2,3,4,5,6,7]
[1,4,7]
```

c. What do you expect the composition of `everyOther` and `everyThird` to give?

e. You likely wrote very similar code for `everyOther` and `everyThird`. By this point in your career, you should have learned that once you find yourself duplicating code, you should factor out the common code.

Write a function, `everyNth N lst`, that selects one of N elements in a list.

f. Rewrite `everyOther` and `everyThird` in terms of `everyNth`.

### Exercise 5: Fun with Patterns

a. Consider the following definition.

```  stupid n = stupid (n-1)
```

Why might this definition be useful in experimenting with programs?

b. As you may recall from our first day of Haskell, one of the nice things about lazy evaluation is that you can define `if` yourself and expect that it behaves the way you would expect. Consider the following definitions.

```  myif True consequent alternate = consequent
myif False consequent alternate = alternate
```

What do you expect the following to produce?

• `myif True (stupid 1) 5`
• `myif True 5 (stupid 1)`
• `myif False (stupid 1) 5`
• `myif False 5 (stupid 1)`

d. Consider the following backwards if

```  bwif consequent alternate True = consequent
bwif consequent alternate False = alternate
```

What do you expect the following to produce?

• `bwif (stupid 1) 5 True`
• `bwif 5 (stupid 1) True`
• `bwif (stupid 1) 5 False`
• `bwif 5 (stupid 1) False`

f. Consider the following definitions.

```  whatever True x = 1
whatever False True = 2
whatever False False = 2
```

What do you expect the following to produce?

• `whatever True (stupid 1)`
• `whatever False (stupid 1)`

h. Consider the following definitions.

```  everwhat x True = 1
everwhat True False = 2
everwhat False False = 2
```

What do you expect the following to produce?

• `everwhat (stupid 1) True`
• `everwhat (stupid 1) False`

## For Those with Extra Time

If you find yourself with extra time, start reading the next section of Tate.

## History

Tuesday, 15 March 2011 [Samuel A. Rebelsky]

• Designed.

Wednesday, 16 March 2011 [Samuel A. Rebelsky]

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Apr 25 08:06:45 2011.
The source to the document was last modified on Wed Mar 16 09:25:39 2011.
This document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/haskell-2.html`.
A PDF version of this document may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CSC302/2011S/Labs/haskell-2.pdf`

You may wish to validate this document's HTML ; ;

Samuel A. Rebelsky, rebelsky@grinnell.edu