---------------------------- 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"
The listMap function applies a binary function to a fixed argument and each element in the list, effectively mapping over the list with a "configured" version of the function.
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a = map (f a)
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
member
The member function checks if an element is in the list using foldl. We start with False (since initially, we haven't found the item), and if we find the item, we change our "accumulator" to True.
member :: Eq e => e -> [e] -> Bool
member x = foldl (\acc item -> if item == x then True else acc) False
selectNApart
The selectNApart function can be implemented by recursively dropping n-1 elements (after taking 1) from the front of the list until the list is empty.
selectNApart :: Int -> [e] -> [e]
selectNApart _ [] = []
selectNApart n xs = head xs : selectNApart n (drop n xs)
Let's go ahead and update the allTests section to run these tests, changing Skip to Run for each.
The functions for questions #5, #6, #7, and #8 should be implemented in their respective files (EvalIntExpr.hs, EvalIdExpr.hs, EvalMaybeExpr.hs, and PostfixExpr.hs) according to their specifications, which have not been provided here.