Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In addition to any functions listed for each exercise, you may always use the following: Tuple construction & component access: (), fst, snd List construction

In addition to any functions listed for each exercise, you may always use the following:

Tuple construction & component access: (), fst, snd

List construction & access: [], (:), head, tail, init, last, (!!), (++)

Boolean operators: not, (&&), (||)

Composition: (.)

Arithmetic: (+), (-), (/), (*)

Comparison: (==), (/=), (<), (<=), (>), (>=)

HOFs: map, foldr, foldl

Implement the following functions using either a left or right fold. For these functions, it's likely that you'll need to pass a tuple into the fold, and extract a value from the (tuple) returned by the fold to return as a result. Endeavor to use point-free style.

gap :: (Eq a) => a -> a -> [a] -> Maybe Int

Returns the integer distance between first appearance of two elements in a list.

Examples:

> gap 3 8 [1..10]

Just 5

> gap 8 3 [1..10]

Nothing

> gap 'h' 'l' "hello"

Just 2

> gap 'h' 'z' "hello"

Nothing

evalExpr :: String -> Int

Evaluates a string containing a simple arithmetic expression using only single digit operands and (binary) + and - operators.

Examples:

> evalExpr "1" 1 > evalExpr "" 0 > evalExpr "1+2+5" 8 > evalExpr "9-5+3-8" -1 

words' :: String -> [String]

Returns the space-delineated words found in a string as a list.

Examples:

> words' "a b c d" ["a","b","c","d"] > words' " hello how are you? " ["hello","how","are","you?"] > words' " " [] 

dropWhile' :: (a -> Bool) -> [a] -> [a]

Removes elements from the beginning of a list that satisfy the predicate.

Examples:

> dropWhile' even [2,4,1,5,6,8] [1,5,6,8] > dropWhile' even [] [] > dropWhile' ((<= 3) . length) $ words' "I am fond of cake" ["fond","of","cake"] 

join :: a -> [[a]] -> [a]

Write a function that joins lists together using a separator value --- the separator should only appear between elements, and not at the end.

Examples:

> join '-' ["hello", "how", "are", "you"] "hello-how-are-you" > join 0 [[1], [2,3]] [1,0,2,3]

Permitted functions: (++), (:)

unzip' :: [(a,b)] -> ([a], [b])

Write a function that takes a list of two-tuples and returns a tuple of two lists -- the first containing the first element of each tuple, and the second the second element (the reverse of "zip").

Examples:

> unzip' [('a',1),('b',5),('c',8)] ("abc",[1,5,8]) 

Permitted functions: (:)

runLengthEncode :: String -> [(Int,Char)]

Run-length encoding is a simple form of data compression that replaces characters in a stream with the count of adjacent occurrences of that character and just a single instance of the character itself. Write a function that takes a string and returns a list of tuples reprenting the run-length encoding of that string.

Examples:

> runLengthEncode "aaaaaaabbb" [(7,'a'),(3,'b')] > runLengthEncode "happy daaay" [(1,'h'),(1,'a'),(2,'p'),(1,'y'),(1,' '),(1,'d'),(3,'a'),(1,'y')] 

Permitted functions: (:), (==), (+)

runLengthDecode :: [(Int,Char)] -> String

Write a function that takes the output of runLengthEncode and returns the original string.

Examples:

> runLengthDecode [(1,'h'), (5,'i')] "hiiiii" > runLengthDecode $ runLengthEncode "whhhhaaaaat?" "whhhhaaaaat?" 

Permitted functions: (++), (:), replicate

vigenere :: String -> String -> String

The Vigenere encryption scheme is similar to the Caesar cipher presented in class in that it makes use of shifting, but instead of a single numeric key applied uniformly to the entire plain text, a string of characters is used as the key. The numeric value of each character (as its position in the alphabet) is used as a shift value, and if the key is shorter than the length of the plain text it is simply repeated.

E.g., to encrypt the plain text "FOOBAR" with the key "BAZ", we can proceed as follows:

Pair each letter of the plain text with a letter from the key:

F O O B A R B A Z B A Z 

Convert each letter to its numeric value (A=0, B=1 ... Z=25)

5 14 14 1 0 17 1 0 25 1 0 25 

Add them together:

6 14 39 2 0 42 

"Wrap" the numbers around so they're in the range 0-25:

6 14 13 2 0 16 

Convert the numbers back into letters:

G O N C A Q 

Write a function that takes a key and plain text and returns the Vigenere encrypted cipher text. Plain text can contain a mix of lowercase and uppercase letters and punctuation, but all letters will be interpreted as uppercase. Punctuation will not be encrypted. The key will contain only letters (lower or upper case), but again will only be interpreted as uppercase.

Examples:

> vigenere "baz" "foobar"

"GONCAQ"

>vigenere "Yadda" "Hello, world!"

"FEOOO, UOUOD!"

Permitted functions: (+), (-), (:), (++), (&&), rem, ord, chr, isLetter, toUpper (to use the last four you'll need to import Data.Char)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Discuss the history of human resource management (HRM).

Answered: 1 week ago