---------------------------- 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"
To implement the functionalities described in your assignment, I'll provide the Haskell code snippets for each commented "TODO" section. You can replace each error "TODO" with the corresponding implementation.
1. toSingletonLists Function
The toSingletonLists function transforms each element of a list into a singleton list using the 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
The listMap function applies a function f to each element of a list, alongside an additional fixed argument a.
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a bs = map (f a) bs
3. member Function
The member function checks if an element is part of a list using foldl.
member :: Eq e => e -> [e] -> Bool
member e = foldl (\acc x -> acc || x == e) False
4. selectNApart Function
The selectNApart function selects every nth element from a list.
selectNApart :: Int -> [e] -> [e]
selectNApart n xs
| n > 0 && not (null xs) = head xs : selectNApart n (drop n xs)
| otherwise = []
Functions in Separate Files
For the functions specified in other files like EvalIntExpr.hs, EvalIdExpr.hs, EvalMaybeExpr.hs, and PostfixExpr.hs, you're expected to implement them according to their respective specifications. Without knowing the specific requirements for each of those functions, I can't provide detailed implementations. However, the concepts used in the solutions above can be leveraged across various implementations:
Use of higher-order functions (map, foldl).
List comprehensions and manipulations (drop, head, and pattern matching on lists).
Testing Main Function
Ensure you also update the main function's test suite statuses from (Skip test...) to (Run test...) for each test you wish to run:
allTests = [
(Run testToSingletonLists),
(Run testListMap),
(Run testMember),
(Run testSelectNApart),
-- Update as necessary when other tests are to be included
]
This modification will include the tests in your main function's test execution, allowing you to verify the correctness of your implementations.