---------------------------- 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"
Let's start by implementing the four functions (toSingletonLists, listMap, member, and selectNApart) and then change the test statuses from Skip to Run to execute their corresponding tests.
Implementation of Functions
1. toSingletonLists Function:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
--------------------------------- 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, (:[]) is a section that creates a singleton list containing the element it's applied to. map applies this function to every element of the input list, producing a list of singleton lists.
2. listMap Function:
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a = map (f a)
The function listMap f a partially applies f to a, resulting in a function that is then mapped over the list using map.
3. member Function:
member :: Eq e => e -> [e] -> Bool
member e = foldl (\acc x -> acc || x == e) False
We use foldl to iterate through the list, starting with False (since initially, we haven't found the element). (\acc x -> acc || x == e) is a lambda function that returns True if either the accumulator acc is True or the current element x equals the target element e.
4. selectNApart Function:
selectNApart :: Int -> [e] -> [e]
selectNApart _ [] = []
selectNApart n (x:xs) = x : selectNApart n (drop (n-1) xs)
This function uses pattern matching to handle the empty list base case. It prepends the first element to the result of recursively calling selectNApart on the list after dropping n-1 elements, effectively selecting every nth element.
Modifying Test Statuses
Now let's uncomment the tests for the implemented functions by changing their status from Skip to Run:
After implementing the functions and modifying the test list, you should now be able to run main to execute the tests for toSingletonLists, listMap, member, and selectNApart.