Site icon Tutor Bin

Haskell Worksheet

Haskell Worksheet

Description

(Make sure that your code compiles and check regularly and make sure that your solutions satisfy the test cases given)
{-Attempt all questions. Make sure that your code compiles and check regularly by loading it into GHCI. You should test your code regularly and make sure that your solutions satisfy the test cases given, note that the test cases don’t cover every scenario but they will give you a good idea of how you are doing. You may define as many helper functions as you like, you may also use any function from the Haskell prelude unless otherwise specified. You may not import any libraries -}

———————————————-

———————————————-

{-1a) Define eqInt which rerturns True when two supplied integers are equal and False otherwise. (1 mark)

eq 5 5 == True

eq 4 5 == False

-}

eqInt::Int -> Int -> Bool

eqInt = undefined

{-1b) Define evList which, using your answer to question 1, returns True when the length of a given list is even. (2 marks)

evList “Clemens” == False

evList “lemens” == True

-}

evList::[a] -> Bool

evList = undefined

{-1c) Using evList define ifList which, if the length of a list is even, returns the first element of the list and the rest of the list otherwise. You should assume that the list is not empty. (3 marks)

ifList [1,2,3,4,5] == Right [2,3,4,5]

ifList [1,2,3,4] == Left 1

-}

ifList::[a] -> Either a [a]

ifList = undefined

{-2a) Using list comprehension build a list of tuples consisting of a number and it’s square. Your function should go up to the value supplied as input. (2 marks)

sqList 2 == [(1,1),(2,4)]

sqList 4 == [(1,1),(2,4),(3,9),(4,16)]

-}

sqList::Int -> [(Int,Int)]

sqList = undefined

{-2b) Define shortenList which takes a list and an element x of the same type. It then counts the number of x’s in the list and removes that number of elements from the end of the list. (3 marks)

shortenList ‘a’ “asdaaldi” == “asdaa”

shortenList 1 [1,2,3,4,5] == [1,2,3,4]

-}

shortenList::Eq a => a -> [a] -> [a]

shortenList = undefined

{-2c) Define reorderList which moves all occurences of a given value in a list to the end of the list. (3 marks)

reorderList ‘a’ “asasasd” == “sssdaaa”

reorderList 1 [1,2,1,3,4,1] == [2,3,4,1,1,1]

-}

reorderList::Eq a => a -> [a] -> [a]

reorderList = undefined

{-2d) By using your solutions to 2b and 2c define remove which removes an element from a list of values. (2 marks)

remove ‘a’ “asdaaldi” == “sdldi”

remove 1 [1,2,3,5,1] == [2,3,5]

-}

remove::Eq a => a -> [a] -> [a]

remove = undefined

{-3 Consider the following type -}

data Val = Zero | One | Two | Three | Four | Five | Six | Seven | Eight | Nine

deriving (Show,Eq)

{-3a) Define val2Int which converts a Val Type to an integer between 0 and 9. The below list may help. (2 marks)

val2Int Two = 2

val2Int Five = 5

-}

help = [Zero,One,Two,Three,Four,Five,Six,Seven,Eight,Nine]

val2Int::Val -> Int

val2Int = undefined

{-3b) Define the type synonym Number which represents an integer as a list of Vals. (1 mark) -}

{-3c) Using the type Number define convert which maps a Number to an Int. You should treat the first element of the list as the most significant number and the last value as the least significant. You should use val2Int to help.(3 marks)

convert [Two,Zero,Two,Zero] == 2020

convert [Two,Six,Zero] = 260

also, convert help = 123456789 — note that it is fine to drop leading zeros

-}

–convert::Number -> Int –uncomment once Number is defined, if you can’t solve 5a) replace Number with [Val]

convert = undefined

{-4) Consider the following type -}

data OddType a = Single a (OddType a) |Tuple (a,a) (OddType a) | Triple (a,a,a) (OddType a) | Done

deriving (Show,Eq)

{-4a) Define hasTrip which searches an element of OddType returning True if it contains a Triple and False otherwise. You should consider pattern matching over each constructor of OddType. (2 marks)

hasTrip (Single 5 (Tuple (5,5) Done)) == False

hasTrip (Single 4 (Triple (4,4,4) Done)) == True-}

hasTrip::OddType a -> Bool

hasTrip = undefined

{-4b)Define findTuples which takes an element of the OddType and returns an Oddtype consisting of only the tuples from the original value. (3 marks)

findTuples (Single 1 (Tuple (2,3) (Single 4 (Tuple (4,5) Done)))) == (Tuple (2,3) (Tuple (4,5) Done))

findTuples (Single 1 (Single 2 (Single 3 (Single 4 Done)))) == Done

-}

findTuples::OddType a -> OddType a

findTuples = undefined

{-4c) Define toList which maps an OddType to it’s corresponding list, you should respect the original placement of values. (3 marks)

toList (Single 1 (Tuple (2,3) (Single 4 (Tuple (5,6) Done)))) == [1,2,3,4,5,6]

toList Done == []

-}

toList::OddType a -> [a]

toList = undefined

{-4d) Provide a functor instance for OddType. (4 marks)

##No test cases as there are many possible defintions. Just make sure fmap works!##

-}

{-5a) Define mayAdd which adds two Maybe Int values together. Your code should return Nothing in the case where either value is Nothing. (1 mark)

mayAdd Nothing (Just 10) == Nothing

mayAdd (Just 20) (Just 0) == (Just 20)

-}

mayAdd::Maybe Int -> Maybe Int -> Maybe Int

mayAdd = undefined

{-5b) Define mayFoldr which performs a right fold across a list of Maybe values, you may not use functions from the Foldable typeclass. (3 marks)

mayFoldr [(Just 5), (Just 4), (Just 3), (Just 2), (Just 1)] mayAdd (Just 0) == Just 15

mayFoldr [(Just 5), (Just 4),Nothing, (Just 2), (Just 1)] mayAdd (Just 0) == Nothing

-}

mayFoldr::[Maybe a] -> (Maybe a -> Maybe b -> Maybe b) -> Maybe b -> Maybe b

mayFoldr = undefined

{-5c) The Either type holds a Left or Right value which can be of different types. Using Either define myIf which takes a bool, two tuples (f::a->b, x::a) and (g::c->d,y::c) using Either, to emulate an if statement – if bool then f a else g c. (2 marks)

myIf True ((+1),5) (tail,”Mytest”) == Left 6

myIf False ((+1),5) (tail,”Mytest”) == Right “ytest”

-}

myIf::Bool -> (a -> b, a) -> (c -> d, c) -> Either b d

myIf = undefined

Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."

Exit mobile version