Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(18 points total) The function twice list should return true iff some value occurs twice in the list. E.g., > filter twice [[],[1],[1,2],[2,2],[1,2,3],[1,2,1],[1,1,2], [1,2,2]] [[2,2],[1,2,1],[1,1,2],[1,2,2]]
(18 points total) The function twice list should return true iff some value occurs twice in the list. E.g., > filter twice [[],[1],[1,2],[2,2],[1,2,3],[1,2,1],[1,1,2], [1,2,2]] [[2,2],[1,2,1],[1,1,2],[1,2,2]] a. (2 points) What is the type of twice? (Include the typeclass.) (4 points) Briefly describe the syntactic and semantic bugs in the program below. (For syntactic errors, don't just parrot the Haskell error messages, give a brief human-understandable description.) b. twice [] = False twice [_] = False twice (x,x] = True twice ( ++ [x] ++ - ++ [y] ++ 2) = x == Y twice (h1 : h2 : t) == (h1 == h2 || twice hi t) b. (3 points) Rewrite twice to make it work. Keep using definition by cases, feel free to add/changel delete cases as you see fit. (3 point) Write a definition by cases for twice that only has two cases (one recursive, one not). (3 points) Rewrite your definition from part (C) using cases and guards, break up the 3-clause logical or test to use a sequence of guards. (Don't leave any || in the definition.) d. twice x pattern | guardl = result1 | guard2 = result2 [1/25: need = not-> ] e. (omitted) (3 points) Rewrite your definition from part (C) to be of the form twice x = case x of .... You can add guards to a case clause using the syntax case expr of pattern | guardl -> result? | guard2 -> result2 (omitted) (18 points total) The function twice list should return true iff some value occurs twice in the list. E.g., > filter twice [[],[1],[1,2],[2,2],[1,2,3],[1,2,1],[1,1,2], [1,2,2]] [[2,2],[1,2,1],[1,1,2],[1,2,2]] a. (2 points) What is the type of twice? (Include the typeclass.) (4 points) Briefly describe the syntactic and semantic bugs in the program below. (For syntactic errors, don't just parrot the Haskell error messages, give a brief human-understandable description.) b. twice [] = False twice [_] = False twice (x,x] = True twice ( ++ [x] ++ - ++ [y] ++ 2) = x == Y twice (h1 : h2 : t) == (h1 == h2 || twice hi t) b. (3 points) Rewrite twice to make it work. Keep using definition by cases, feel free to add/changel delete cases as you see fit. (3 point) Write a definition by cases for twice that only has two cases (one recursive, one not). (3 points) Rewrite your definition from part (C) using cases and guards, break up the 3-clause logical or test to use a sequence of guards. (Don't leave any || in the definition.) d. twice x pattern | guardl = result1 | guard2 = result2 [1/25: need = not-> ] e. (omitted) (3 points) Rewrite your definition from part (C) to be of the form twice x = case x of .... You can add guards to a case clause using the syntax case expr of pattern | guardl -> result? | guard2 -> result2 (omitted)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started