Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Haskell Programming: Simply answer the question (Code) Problems [50 points total] Haskell Programming 1. (5 points] Using head and reverse, define a function (i.e., write

Haskell Programming: Simply answer the question (Code)

image text in transcribed

image text in transcribed

Problems [50 points total] Haskell Programming 1. (5 points] Using head and reverse, define a function (i.e., write the definition for a function) last' that mimics last: It takes a list and return the last element. E.g. last' [1,2,3]- 3. You should cause a runtime error if you're passed the empty list, but it doesn't have to be the same error message as last. 2. 8 points] Define a function head_repeats n x that returns True if the first n elements of x equals the second n elements of X. Eg., head-repeats 2 [ 3 , 5 , 3 , 5, 71 should return True. If n s 0, return True. 3 [10 points] What's wrong with the following definition? Suggest a change to the definition that changes the arguments of f but preserves the spirit of the definition below. Also give the type of your new function f. f 01 4.[15 points] Define a function swap_ends that takes a list and returns the same list but with the first and last elements swapped. E.g., swap_ends "hello" - 'oellh" (remember, strings are lists of characters). Use function definition by cases to ensure that swap-ends [ ] = [ ] and swap-ends [x] = [xj. (Le., return empty and singleton lists as is). 5. 8 points] Here's a buggy definition for the general case of swap_ends. Briefly explain the nature of the bug and why we can't eliminate the bug if we rewrite the left hand side of the definition but use the same right-hand side of the definition. Two or three sentences is enough swap_ends ([x]+ y ++ [z])-[z] y tt [x] 6 4 points] What is wrong with the definition below? Give a corrected definition of f. Explain briefly f :: (Num a) => a-> [a] fx=[x / 2]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database 11g SQL

Authors: Jason Price

1st Edition

0071498508, 978-0071498500

More Books

Students also viewed these Databases questions