Haskell Assignment 1: Exercises
Fall 2010, David Matuszek

Purposes of this assignment


Write and test the following functions:

  1. A function countLetters that, given a string as an argument, counts the number of each letter in the string and returns the result as a list of 26 integers. Capital letters and lowercase letters should be counted as the same. Characters that are not letters should be ignored.


        countLetters "Amazingly few discotheques provide jukeboxes."
        countLetters "ABRAcadabra"

    Suggested approach: Don't start out by trying to do the whole thing at once! Play around with some of the higher-order functions, and try things out at the Haskell prompt. Start with smaller pieces (can you count how many times a particular letter occurs?). Turning it into a function and testing the function should be your last step.

  2. Write a function tr that takes three strings as arguments. Assume that the first and second strings are the same length (that is, don't do any error checking). In the third string, replace each occurrance of a character in the first string with the corresponding character in the second string.


        tr "aeiou" "AEIOU" "Amazingly few discotheques provide jukeboxes."
        AmAzIngly fEw dIscOthEqUEs prOvIdE jUkEbOxEs.
        tr "aeiou" "_____"
        "Amazingly few discotheques provide jukeboxes."
        Am_z_ngly f_w d_sc_th_q__s pr_v_d_ j_k_b_x_s.>
        tr " ." "-!" "Amazingly few discotheques provide jukeboxes."
  3. Write a function rot13 that takes one string as argument, and returns the string will all letters replaced by the letter 13 places along, end around. Lowercase letters should remain lowercase, capital letters should remain capitals, and nonletters should be unchanged.


        rot13 "Amazingly few discotheques provide jukeboxes."
        Nznmvatyl srj qvfpbgurdhrf cebivqr whxrobkrf.
        rot13 "Nznmvatyl srj qvfpbgurdhrf cebivqr whxrobkrf."
        Amazingly few discotheques provide jukeboxes.

    Rot13 is a very well known, very low security code. It is often used to obscure potentially offensive jokes, so that no one who might be offended will accidentally read them.

Note: Once you have a basic understanding of Haskell, these are not difficult functions. The first can be done in a single line; the second one took me six lines total (four of them in an auxiliary function). Your programs do not have to be this short, but if you find your program getting significantly longer than this, you may want to stop and reconsider your approach.

Due date:

Turn in your text.hs file by midnight, Tuesday November 9. As only one file is required, there is no need to zip it.