2025-10-20 Daily Worksheet
Review
-
Inside a function
f x, useletto create variables x2 holding x squared, and x3 holding x cubed. Then create the function below.$$ f(x) = x^3 (x^2-1) $$
Semi-New
An official data type is Maybe a. The data definition is:
data Maybe a = Nothing | Just a
deriving (Show, Eq, ...)
Do not recopy that code; it is built into the Haskell interpreter that we use.
-
Write a function
g2that takes in aMaybe Intand returns 0 if it is givenNothingand otherwise 10 more than the absolute value of the int.checks_2 = [ 0 == g2 Nothing , 12 == g2 (Just 2) , 25 == g2 (Just 15) ] -
Write a function
h2that takes in aMaybe Stringand puts out aMaybe String, doubling the string if it exists.check_h2 = [ Just "mommom" == h2 (Just "mom") , Nothing == h2 Nothing ]
Higher Order Functions
-
(
myor) Theorfunction returnsTruewhen any item in a list isTrue; otherwise it returnsFalse. Usefoldlto create your own version of this function.myor :: [Bool] -> Bool -
Use recursion to write a function
myallthat applies a function to every item in a list, and returnsTrueif every function application returnsTrue.myall :: (a -> Bool) -> [a] -> Bool check_myall = [ myall even [2,4,10,20] == True ,myall (<5) [1,3,6,2,4] == False ]