CSC 8310 Linguistics of Programming Languages
Dr. David Matuszek
Fall 1997, Villanova University

Haskell Assignment

Write and test the following two functions:

1. Write 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.

Examples:

    countLetters "Amazingly few discotheques provide jukeboxes."
    [2, 1, 1, 2, 6, 1, 1, 1, 3, 1, 1, 1, 1, 1, 3, 1, 1, 1, 3, 1, 2, 1, 1, 1, 1, 1]
    

  countLetters "ABRAcadabra" [5, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0]

 

2. Write a function tr that takes three strings as arguments. Assume that the first and second strings are the same length. In the third string, replace each occurrance of a letter in the first string with the corresponding letter in the second string.

Examples:

    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." Amazingly-few-discotheques-provide-jukeboxes!

 

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). Brevity is not a requirement; but if you find your program getting significantly longer than this, you may want to stop and reconsider your approach.