module Main (main) where import TestUtils import EvalIntExpr (testEvalIntExpr) import…
- User
module Main (main) where
import TestUtils import EvalIntExpr (testEvalIntExpr) import EvalIdExpr (testEvalIdExpr) import EvalMaybeExpr (testEvalMaybeExpr) import PostfixExpr (testPostfixExpr)
import Test.QuickCheck
---------------------------- 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"
testToSingletonLists = do print "******* toSingletonLists" quickCheck toSingletonLists [ 5, 7, 2 ] == [ [5], [7], [2] ] quickCheck toSingletonLists [ 'a', 'x', 'd' ] == [ "a", "x", "d" ] quickCheck toSingletonLists [] == ([] :: [[Int]])
--------------------------------- 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"
testListMap = do print "******* listMap" quickCheck listMap (+) 5 [1, 2, 3] == [6, 7, 8] quickCheck listMap (-) 5 [1, 2, 3] == [4, 3, 2] quickCheck listMap (*) 5 [1, 2, 3] == [5, 10, 15] quickCheck listMap (-) 5 [] == []
---------------------------------- 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 member 5 [ 5, 7, 2 ] == True quickCheck member 2 [ 5, 7, 2 ] == True quickCheck member 7 [ 5, 7, 2 ] == True quickCheck member 4 [ 5, 7, 2 ] == False quickCheck 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"
testSelectNApart = do print "******* selectNApart" quickCheck selectNApart 2 [0..10] == [0, 2, 4, 6, 8, 10] quickCheck selectNApart 2 ['a'..'z'] == "acegikmoqsuwy" quickCheck selectNApart 3 [0..20] == [0, 3, 6, 9, 12, 15, 18] quickCheck selectNApart 5 [0..21] == [0, 5, 10, 15, 20] quickCheck selectNApart 5 ([]::[Int]) == []
------------------------ Functions in Separate Files --------------------
-- #5: 15-points -- implement the specified function in EvalIntExpr.hs
-- #6: 15-points -- implement the specified function in EvalIdExpr.hs
-- #7: 20-points -- implement the specified function in EvalMaybeExpr.hs
-- #8: 20-points -- implement the specified function in PostfixExpr.hs
--------------------------------- Tests ---------------------------------
-- 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
- Assistant
To complete the assignments, we need to implement the functions
toSingletonLists,listMap,member, andselectNApart. Let's fill in these implementations based on the provided specifications.Implementation of
toSingletonListsAs per the instructions, we must use the
mapfunction to transform a list of elements into a list of singleton lists. A lambda could be used, but the section hint suggests a simpler approach.toSingletonLists :: [e] -> [[e]] toSingletonLists = map (:[])Implementation of
listMapFor this function, a list comprehension could be used, but since the hint suggests using
map, let's stick to it.mapneeds to applyf ato each element in the list, which makes it straightforward.listMap :: (a -> b -> c) -> a -> [b] -> [c] listMap f a bs = map (f a) bsImplementation of
memberImplementing
memberrequires the use offoldl. This function accumulates a boolean value indicating whether the element has been found in the list.member :: Eq e => e -> [e] -> Bool member e = foldl (\acc x -> if x == e then True else acc) FalseImplementation of
selectNApartFinally, for
selectNApart, the hint suggests usingdrop. A recursive approach could accommodate both the dropping and the selection efficiently.selectNApart :: Int -> [e] -> [e] selectNApart _ [] = [] selectNApart n xs = head xs : selectNApart n (drop n xs)Lastly, to ensure the test suites for the implemented functions do run, you need to change their status from
SkiptoRunin theallTestslist.allTests = [ (Run testToSingletonLists), (Run testListMap), (Run testMember), (Run testSelectNApart), -- Keep other tests as they are unless implemented ]Ensure you integrate the above implementations and adaptations into your module code properly, and you should be good to progress with testing your implementations.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →
- gpt-4-0125-previewCompare side by side →