Jump to content

First-class functions: Difference between revisions

→‎{{header|Haskell}}: Added type annotations on functions
(→‎{{header|Haskell}}: Added type annotations on functions)
Line 1,240:
 
=={{header|Haskell}}==
<lang haskell>Prelude>cube let:: cubeFloating xa => xa ^-> 3a
Prelude> let crootcube x = x ** (1/3).0
 
Prelude> let compose f g = \x -> f (g x) -- this is already implemented in Haskell as the "." operator
croot :: Floating a => a -> a
Prelude> -- we could have written "let compose f g x = f (g x)" but we show this for clarity
croot x = x ** (1/3)
Prelude> let funclist = [sin, cos, cube]
 
Prelude> let funclisti = [asin, acos, croot]
Prelude> let compose f g = \x -> f (g x) -- this iscompose already implementedexists in Haskell as the "`."` operator
Prelude> zipWith (\f inversef -> (compose inversef f) 0.5) funclist funclisti
-- compose :: (a -> b) -> (b -> c) -> a -> c
[0.5,0.4999999999999999,0.5]</lang>
-- compose f g = \x -> g (f x)
 
funclist :: Floating a => [a -> a]
Prelude> let funclist = [sin, cos, cube ]
 
invlist :: Floating a => [a -> a]
Prelude>invlist let funclisti = [asin, acos, croot]
 
main :: IO ()
main = print $ zipWith (\f i -> f . i $ 0.5) funclist invlist </lang>
{{output}}
<pre>[0.5,0.4999999999999999,0.55000000000000001]</langpre>
 
=={{header|Icon}} and {{header|Unicon}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.