main = do
mapM_ id tests
where
only = onlyTests allTests
tests = if (length only > 0) then only else runTests allTests
For toSingletonLists use the map function to transform a list of elements e into a list of singleton lists [e]. Must use map only.
Hint: use map with a section.
For listMap return a list containing f a b for each element b in list.
Hint: use the map function or a list comprehension
For member use foldl to implement member e list which returns True if e is a member of list. Must use foldl only, cannot use elem
Hint: define folding function using a lambda or local let/where definition.
For selectNApart, 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, and so on
Hint: use drop
For Tests, 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.
Ensure that all test suites are set to Run.
(:)
e
[e]
listMap
This function can be implemented in two ways, using map or a list comprehension. Here, we'll use map.
haskell
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a = map (f a)
This works by applying the function f partially with a, then mapping the result over each element of the list.
member
Using foldl to implement member:
haskell
member :: Eq e => e -> [e] -> Bool
member e = foldl (\acc x -> if x == e then True else acc) False
We use a lambda function with foldl that checks whether the current element equals e and updates the accumulator to True if it does. If not, the accumulator retains its value. Initially, the accumulator is False.
selectNApart
The hint suggests using drop, so it looks like we'll be employing a recursive approach, dropping n elements each time:
haskell
selectNApart :: Int -> [e] -> [e]
selectNApart _ [] = []
selectNApart n (x:xs) = x : selectNApart n (drop (n-1) xs)
For each recursive call, we skip n-1 elements (since we also count the current element x that we're adding to the result) and add the next element to the result.
Updating the Test Suites Status
To ensure all test suites are set to run, replace (Skip test... with (Run test... for all test functions in allTests array: