Question: Haskell Programming - Code Do it in Haskell GHCI Function Definitions; Patterns; Case Expressions 3. (18 points total) The function twice list should return true
Haskell Programming - Code
Do it in Haskell GHCI


Function Definitions; Patterns; Case Expressions 3. (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]] (2 points) What is the type of twice? (Include the typeclass.) a. b. (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.) :{ twice [ ] = False twice [_] = False twice (x,x] = True twice ( ++ [x] ++ - ++ [y] ++ 2 ) = x == y twice (hl : 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. c. (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.) twice x pattern | guard1 = result1 [1/25: need = not ->] | guard2 = result2 (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 e. case expr of pattern | guardl -> result1 | guard2 -> result2 (omitted) Function Definitions; Patterns; Case Expressions 3. (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]] (2 points) What is the type of twice? (Include the typeclass.) a. b. (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.) :{ twice [ ] = False twice [_] = False twice (x,x] = True twice ( ++ [x] ++ - ++ [y] ++ 2 ) = x == y twice (hl : 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. c. (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.) twice x pattern | guard1 = result1 [1/25: need = not ->] | guard2 = result2 (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 e. case expr of pattern | guardl -> result1 | guard2 -> result2 (omitted)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
