Question: could you please answer the following exercises in Haskell programming and test them: --1) Type synonyms for words and lines. (Prime added to textbook version
could you please answer the following exercises in Haskell programming and test them:
--1) Type synonyms for words and lines. (Prime added to textbook version -- to avoid name clash.) type Word' = String type Line' = [Word'] -- The "whitespace" characters. whitespace :: String whitespace = [' ','\t',' ']
--2) Get a word from the front of a string. getWord :: String -> String getWord [] = [] getWord (x:xs) | elem x whitespace = [] | otherwise = x : getWord xs
--3) Drop the first word of a string. dropWord :: String -> String dropWord [] = [] dropWord (x:xs) | elem x whitespace = (x:xs) | otherwise = dropWord xs
--4) Remove the whitespace character(s) from the front of a string. dropSpace :: String -> String dropSpace [] = [] dropSpace (x:xs) | elem x whitespace = dropSpace xs | otherwise = (x:xs)
--6) Split a string into words. splitWords :: String -> [Word'] splitWords st = split (dropSpace st) split :: String -> [Word'] split [] = [] split st = (getWord st) : split (dropSpace (dropWord st))
--7) Split into lines of length at most lineLen. lineLen :: Int -- lineLen = 80 lineLen = 10 -- for testing purposes
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
