$25
4.1
1. Unzip the A4.zip folder. You should find 2 folders, each with one file inside:
hs, containing Solutions.hs - for the Haskell exercises sml, containing solutions.sml - for the SML exercises
2. Edit the first line of each of the source files as described in the comments.
3. Edit the source files with your solutions.
4. When done, zip (not rar renamed as zip!) this A4 folder and name the zip archive with the following format:
A4 hFirstNamei hLastNamei hGroupi
Examples of valid names:
A4 John Doe 30432.zip
A4 Ion Popescu 30434.zip
A4 Gigel-Dorel Petrescu 30431.zip
Examples of invalid names:
Solutions.zip
A4.zip
Solutii A4 Ion Popescu.zip
4.2 Assignment exercises
4.2.1 Haskell
Exercise 4.2.4 7p
Define a function topWords n str , with the signature topWords :: Int -> String -> [(String, Int)] that returns the top n words from the string str , by the number of occurrences. If there are multiple words with the same number of occurrences, you should break the ties sorting the words in lexicographic order. You can assume that the input string contains only letters and spaces.
Haskell REPL
> topWords 10 "I know that you know that I know"
[("know", 3), ("I", 2), ("that", 2), ("you", 1)]
> topWords 3 "I know that you know that I know"
[("know", 3), ("I", 2), ("that", 2)]
> topWords 6 "An apple a day keeps the doctor away"
[("An", 1), ("a", 1), ("apple", 1), ("away", 1), ("day", 1), ("doctor", 1)]
Solution option 1:
One possible solution is to define a function update fn def k l with the signature udpate :: (Eq k) => (v -> v) -> v -> k -> [(k, v)] -> [(k, v)] that looks up the key k in the association list l . If the key is found, then the update function fn is applied to the value associated to the key and the list with the update value is returned. If the key is not found, the key is inserted into the association list with the default value def .
Haskell REPL
> update (+1) 1 "a" [("a", 2), ("b", 3), ("c", 1)] [("a", 3), ("b", 3), ("c", 1)]
> update (+1) 1 "d" [("a", 2), ("b", 3), ("c", 1)] [("a", 3), ("b", 3), ("c", 1), ("d", 1)]
> update (+1) 1 "c" [("a", 2), ("b", 3), ("c", 1)]
[("a", 3), ("b", 3), ("c", 2)]
4.2.2 SML