Produce a list with n identical elements:
replicate :: Int -> a -> [a]
replicate n x = undefined
Select the nth element of a list:
(!!) :: [a] -> Int -> a
xs !! i = undefined
Decide if a value is an element of a list:
elem :: Eq a => a -> [a] -> Bool
merge :: Ord a => [a] -> [a] -> [a]
that merges two sorted lists of values to give a single sorted list. For example:
Prelude λ> merge [2,5,6] [1,3,4]
[1,2,3,4,5,6]
msort :: Ord a => [a] -> [a]
that implements merge sort, which can be specified by the following two rules:
Express the comprehension
[f x | x xs, p x]
using the functions map
and filter
.
Redefine map f
and filter p
using foldr
. (Hint: What is the type of map f
? And how about the type of filter p
?)
Convert (8729)_{12} into its octet (base-8) representation.
Write a haskell function that converts any base-8 (octet) number into its base-6 (senary) form.
octet2senary :: [Int] -> [Int]
octet2senary xs = undefined
For example, to obtain the senary representation of (345)_{8}, you can call
Prelude λ> octet2senary [3,4,5]
[1,0,2,1]
to get (1021)_{6}.
Write a Haskell function that adds two base-8 numbers represented as lists. For example, to add (6273)_{8} and (7712)_{8} in ghci
, you type
Prelude λ> addoctets [6,2,7,3] [7,7,1,2]
[1,6,2,0,5]
You can use the following code stub to get yourself started.
addoctets :: [Int] -> [Int] ->[Int]
addoctets = undefined
b
numbers.
mult :: Int -> [Int] -> [Int] ->[Int]
mult = undefined
b
numbers.
divmod :: Int -> [Int] -> [Int] ->[Int]
divmod = undefined