---------------------------- 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
Here, we need to apply a binary function f between a single value a and each element b in a list, effectively using map.
haskell
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a = map (f a)
member
We need to check if an element e is present in a list. This function returns True if e is a member of the list, and False otherwise, using foldl.
haskell
member :: Eq e => e -> [e] -> Bool
member e = foldl (\acc x -> acc || x == e) False
selectNApart
The function selects elements from a list at intervals of n steps.
haskell
selectNApart :: Int -> [e] -> [e]
selectNApart n = map snd . filter ((== 0) . (`mod` n) . fst) . zip [0..]
This implementation first pairs each element with its index using zip [0..], filters those pairs where the index mod nequals0`, and then maps the result to obtain just the values (discarding the indexes).
However, my solution for selectNApart might be incorrect because it doesn't utilize drop as hinted. If the intention is specifically to use drop, here’s an alternative version that more directly follows this approach:
haskell
selectNApart :: Int -> [e] -> [e]
selectNApart n xs
| n > 0 = case drop (n - 1) xs of
(y:ys) -> y : selectNApart n ys
[] -> []
| otherwise = []
This revised function recursively processes the list, each time dropping n-1 elements and selecting the next one, until the list is empty.
Please, integrate each function into your codebase as necessary and ensure to replace the stubs with these implementations. Remember, depending on the precise contexts and modules where you plan to use these functions, additional adjustments might be required.
Lastly, ensure to remove the Skip tag beside the test calls for these implemented functions in your allTests list to enable running these tests.