---------------------------- toSingletonLists ---------------------------
-- #1: 5-points
-- Use the map function to transform a list of elements e into
-- a list of singleton lists [e]. Must use map.
-- Hint: use map with a section.
toSingletonLists :: [e] -> [[e]]
toSingletonLists _ = error "TODO"
--------------------------------- listMap -------------------------------
-- #2: 5-points
-- Return a list containing f a b for each element b in list.
-- Hint: use the map function or a list comprehension
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap _ _ _ = error "TODO"
---------------------------------- member -------------------------------
-- #3: 10-points
-- Use foldl to implement member e list which returns True iff
-- e is a member of list. Must use foldl, cannot use elem
-- Hint: define folding function using a lambda or local let/where definition;
-- also see definition of member in slides.
member :: Eq e => e -> [e] -> Bool
member _ _ = error "TODO"
testMember = do
print "******* member"
quickCheck counterexample"intsfirst" member 5 [ 5, 7, 2 ] == True
quickCheck counterexample"intslast" member 2 [ 5, 7, 2 ] == True
quickCheck counterexample"intsmid" member 7 [ 5, 7, 2 ] == True
quickCheck counterexample"intsfail" member 4 [ 5, 7, 2 ] == False
quickCheck counterexample"empty" member 4 [] == False
------------------------------- selectNApart ----------------------------
-- #4: 10-points
-- Given an Int n and a list of elements e, return a list
-- containing the elements of the list at index 0, n, 2n, 3n, 4n, ...
-- Hint: use drop
selectNApart :: Int -> [e] -> [e]
selectNApart _ _ = error "TODO"
-- Can mark test suites with following test statuses:
-- Only: run only these tests and other tests marked Only.
-- Run: run these tests when no tests are marked Only.
-- Skip: skip these tests.
allTests = [
(Skip testToSingletonLists),
(Skip testListMap),
(Skip testMember),
(Skip testSelectNApart),
(Skip testEvalIntExpr),
(Skip testEvalIdExpr),
(Skip testEvalMaybeExpr),
(Skip testPostfixExpr)
]
main = do
mapM_ id tests
where
only = onlyTests allTests
tests = if (length only > 0) then only else runTests allTests
Let's implement the solutions to the exercises defined in your Haskell module. I'll walk through implementations for each of the given exercises, providing explanations as necessary.
Exercise 1: toSingletonLists
The task is to transform a list of elements into a list of singleton lists containing these elements. We can achieve this by using the map function, which applies a function to each element of a list. We simply need to turn each element e into a list [e].
In this implementation, (:[]) is a section that takes a single element and constructs a singleton list with that element. This effectively transforms each element of the input list into a singleton list.
Exercise 2: listMap
This function should apply a binary function to a fixed value and each element of a list. We can utilize map to achieve this by partially applying the binary function to the fixed value, thus creating a unary function suitable for use with map.
haskell
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a = map (f a)
Here, (f a) is the partial application of f to a, yielding a unary function that takes a b and returns a c. We then map this function over the list of b values to get the desired result.
Exercise 3: member
We will implement member using foldl. The idea is to accumulate a Boolean value that tracks whether the element has been found in the list.
haskell
member :: Eq e => e -> [e] -> Bool
member e = foldl (\acc x -> acc || x == e) False
The lambda function (\acc x -> acc || x == e) checks if the current element x is equal to e and updates the accumulator accordingly. The fold starts with False indicating that e has not been found.
Exercise 4: selectNApart
To get elements from the list at intervals defined by n, we use drop and recursion. We repeatedly drop n - 1 elements after taking each element to skip to the next desired element, stopping when the list is empty.
haskell
selectNApart :: Int -> [e] -> [e]
selectNApart n = go where
go [] = []
go (x:xs) = x : go (drop n xs)
In this recursive definition, go is a helper function that processes the list. For each recursion step, it takes the first element and then skips n elements (using drop) before continuing.
Running Tests
Don't forget to uncomment the test function calls related to the implemented functions in the main function or adjust their status from Skip to Run in allTests, so you can validate your implementations.
Notes
This covers the first four exercises. As for exercises 5 to 8, since they refer to implementations in separate files (EvalIntExpr.hs, EvalIdExpr.hs, EvalMaybeExpr.hs, and PostfixExpr.hs), you'll need to apply similar programming principles to those tasks based on the specified requirements in those exercises.