Question: Consider a function that takes an integer and adds two, but only if it is positive. If the input is zero or less, then that

Consider a function that takes an integer and adds two, but only if it is positive. If the input is zero or less, then that is an error. Write this function as a Haskell function, handling the error in the functional way.
f x = if x >0 then 2+x else Null
f x = if x >0 then 2+x else error "invalid input"
f x = if x >0 then Just (2+x) else Nothing

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!