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.
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, 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.
We use the map function or a list comprehension to apply a function to each element of a list together with an additional argument, returning a list of the results.
haskell
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a = map (f a)
Task: member
To implement member using foldl, we start with False as the initial accumulator value, and set it to True if we find the element.
haskell
member :: Eq e => e -> [e] -> Bool
member x = foldl (\acc y -> acc || x == y) False
Task: selectNApart
We repeatedly drop n elements from the list and take the first element of what remains, building the result list.
haskell
selectNApart :: Int -> [e] -> [e]
selectNApart n xs = case drop n xs of
[] -> []
(y:ys) -> y : selectNApart n ys
Setting Test Suites to Run
Lastly, we need to ensure all test suites are set to Run instead of Skip.
For selectNApart, the requirement was poorly interpreted. To correct this, it's essential to adjust it according to the correct logic prescribed in the hint. The expectation to select elements at indices 0, n, 2n, etc., is better met by utilizing list indexing or generating these indices and mapping them to the list, not by using drop as initially described.
Correct implementation of selectNApart using list filtering by index:
haskell
selectNApart :: Int -> [e] -> [e]
selectNApart n xs = [x | (x, i) <- zip xs [0..], i `mod` n == 0]
This approach pairs each element with its index, filters where the index is divisible by n, and then extracts the elements.