Chapter 2 Reading Guide

Haskell Starting Out Questions

A. Sections 2.1–2.3

  1. There is an annoyance with negative numbers that is mentioned.

    • What is the correct way to write five times negative three in Haskell?
    • What is a wrong way to do the same thing?
  2. Logical operations are basic building blocks of programs. How do you write:

    • and
    • or
    • not
    • equals
    • not equals
  3. Haskell follows the normal order of operations from mathematics. However, in math functions must be written f(x) with parentheses, whereas in Haskell functions can be written f x without parentheses. This means we need to fit functions into the order of operations.

    • Find the values of sqrt (2 + 7) and (sqrt 2) + 7. Find sqrt 2 + 7. Where is Haskell putting the parentheses?
    • Find the values of sqrt (9 * 2) and (sqrt 9) * 2. Find sqrt 9 * 2. Where is Haskell putting the parentheses?
    • Define x = sqrt(pi). Find the values 0.1+(sin x)^2 and 0.1+sin(x^2). How does Haskell interpret 0.1 + sin x^2?
    • Where does function calling fit in the order of operations?
    • Give an example of a problem that this could create. (That is, the program does not do what a math student would expect it to do.)

    Having trouble answering this question? Search the chapter for the word “precedence”.

  4. Write the function hypSquare a b that computes the square of the length of the hypotenuse of a right triangle with legs a and b.

  5. The apostrophe character (') is usually used for one of two purposes. (Also: see the Tidy Details section at the end.) List both:

    1. Purpose 1:
    2. Purpose 2:
  6. Are you allowed to use the apostrophe in function or variable names?

  7. In Racket you used (cons x listy) or (list* x listy) to make a list with x before an existing list. How do you do that in Haskell?

  8. In Racket you used (concatenate firstStr secondStr) to put two strings together. How do you do that in Haskell?

  9. Slightly longer exercise:

    1. Make a list learn containing the words “this” “is” “all” “good”.
    2. Make a new list called learnMore containing “Hopefully” “this” “is” “all” “good”
    3. What is learnMore !! 4.
  10. Complete the table of equivalents

Racket Haskell language equivalent functions
first
rest
length
empty?
  1. What are the signatures and purposes of these functions?

    1. init
    2. last
    3. take
    4. drop
  2. How can you ask the computer if the list called xs contains the number 5?

  3. There are several more functions that are used less often, but still make it into the basic starting out chapter. Write quick summaries as necessary.

    1. reverse
    2. maximum
    3. minimum
    4. sum
    5. product

B. Sections 2.4–2.6

  1. Create a list of integers from 1 to 1000 (inclusive).

  2. Create a list of letters from 'm' to 'a' in that order.

  3. New functions: write signature and purpose for each.

    1. cycle
    2. repeat
    3. replicate
  4. Write a function that removes all of the vowels from a word.

  5. What is the main difference between a tuple and a list?

  6. New functions: write signature and purpose for each.

    1. fst
    2. snd
    3. zip
  7. Write a function that takes in a list of words and puts out a list of ordered pairs (word, length of word).

  8. Write a function that takes in a list of words and puts out a list with the second word doubled in its own list, the third word tripled in its own list, etc.

For example, the function call

amplify ["hill","rock","star","four"]

should produce

[["hill"],["rock","rock"],["star","star","star"], ["four","four","four","four"]]

Tiny Details

The apostrophe is on the key with the double quote (on a US keyboard), so look for ' and " together. The grave accent is on a key with the tilde, so look for ~ and ` together. Minutae: The difference between the apostrophe and the grave accent is hard to see in most typefaces, but they mean totally different things to Haskell. Do not mix them. Some word processors (Google Docs) also convert apostrophe to a curly quote (the “Smart Quotes” feature). Haskell does not understand the smart single quote at all.

Last modified August 18, 2023: 2022-2023 End State (7352e87)