- (a)
[1 mark]
Write a
recursive
function
maxList :: Ord a => [a] -> a
that returns the largest value
held in the argument list.
You can assume that there is always
at least one element in the list.
For example:
maxList [1]
)
1
maxList [1,2,5,4,2,4]
)
5
maxList [1,2,5,4,2,4,10]
)
10
Hint:
you may wish to use the standard prelude function
max :: Ord a => a -> a -> a
(b) [
1 mark
] Recode the list maximum problem to use
foldl
. Call
your function
maxListF
. This function will
not
recursively call
itself.
Write the function
initialMatch :: String -> String -> Maybe (String,String
).
The call
initialMatch
pat str
will return
Nothing
if
pat
does not ap-
pear at the beginning of
str
; otherwise it will return
Just (
pat
,
rest
)
where
rest
is the remainder of the
str
after the initial
pat
is removed.
For example:
3. Write the function
longestMatch :: [String] -> String -> Maybe (String,String
).
The call
longestMatch
pats str
will return
Nothing
if none of the
strings in
pats
appear at the beginning of
str
; otherwise it will return
Just (
pat
,
rest
)
where
pat
is the
longest
matching pattern in
pats
that matches the beginning of
str
, and
rest
is the remainder of the
str
after the initial
pat
is removed.
4. Write the function
freq :: String -> -> [(Char,Int)]
.
The call
freq
str
will return a list of tuples. There will be one tuple
for each
unique alphabetic
character in
str
and the accompanying in-
teger records the number of occurrences of the character. The tuple
list is sorted in ascending order of character, which is reported as the
lower case representation.
5. Write the function
freq2 :: String -> -> [(Int,[Char])]
.
Like
freq
, the function
freq2
counts frequency of occurrence of al-
phabetic characters. However it reports the results dierently.