Use the Glasgow Haskell Compiler ghc to implement all the functions specified in Main.hs and in the skeleton files referenced by Main.hs.
When a function requirement specifies a restriction on how that function should be implemented, your implementation must meet that restriction.
Main.hs
A skeleton file which contains specifications for the some of the functions you are required to write. For each function it contains a dummy implementation with a "TODO" error. It also contains QuickCheck tests for these functions as well as a main function which runs all of the tests for the entire project.
module Main (main) where
---------------------------- 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"
Below are the implementations for the specified functions in Main.hs. Since the implementation of functions in separate files such as EvalIntExpr.hs, EvalIdExpr.hs, EvalMaybeExpr.hs, and PostfixExpr.hs requires the content and specifications from those files which are not provided, I'll only focus on the functions here.
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, I used the section (:[]) to construct a singleton list containing each element.
Implementation for listMap
listMap :: (a -> b -> c) -> a -> [b] -> [c]
listMap f a = map (f a)
This makes use of the map function to apply a function f that takes two arguments where the first argument is fixed as a.
Implementation for member
member :: Eq e => e -> [e] -> Bool
member e = foldl (\acc x -> acc || x == e) False
Leveraging foldl to accumulate a True value if the element e is found in the list. The lambda function (\acc x -> acc || x == e) is used to carry the accumulated result.
Implementation for selectNApart
selectNApart :: Int -> [e] -> [e]
selectNApart n = map head . takeWhile (not . null) . iterate (drop n)
This approach uses iterate to repeatedly apply drop n to the list, generating a list of lists where each sublist skips n elements from the previous one. Then, map head extracts the first element (if present) of each sublist. Finally, takeWhile (not . null) ensures we only consider non-empty sublists.
Testing Entry In Main Function
To have the implementations tested, you need to mark the corresponding test suites to Run or Only instead of Skip in the allTests definition within Main.hs. Here's how you could update the allTests list to run all tests:
allTests = [
(Run testToSingletonLists),
(Run testListMap),
(Run testMember),
(Run testSelectNApart)
-- Assuming other test implementations are skipped as they require separate file implementations
]
Then, executing the main function will run these updated tests.