First-class functions: Difference between revisions

From Rosetta Code
Content added Content deleted
m (task)
(added ocaml and haskell)
Line 16: Line 16:


The Python example conforms to the above.
The Python example conforms to the above.

=={{header|Haskell}}==
<lang haskell>Prelude> let cube x = x ^ 3
Prelude> let croot x = x ** (1/3)
Prelude> let compose f g = \x -> f (g x) -- this is already implemented in Haskell as the "." operator
Prelude> -- we could have written "let compose f g x = f (g x)" but we show this for clarity
Prelude> let funclist = [sin, cos, cube]
Prelude> let funclisti = [asin, acos, croot]
Prelude> zipWith (\f inversef -> (compose inversef f) 0.5) funclist funclisti
[0.5,0.4999999999999999,0.5]
</lang>

=={{header|OCaml}}==
<lang ocaml># let cube x = x ** 3.;;
val cube : float -> float = <fun>
# let croot x = x ** (1. /. 3.);;
val croot : float -> float = <fun>
# let compose f g = fun x -> f (g x);; (* we could have written "let compose f g x = f (g x)" but we show this for clarity *)
val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun>
# let funclist = [sin; cos; cube];;
val funclist : (float -> float) list = [<fun>; <fun>; <fun>]
# let funclisti = [asin; acos; croot];;
val funclisti : (float -> float) list = [<fun>; <fun>; <fun>]
# List.map2 (fun f inversef -> (compose inversef f) 0.5) funclist funclisti;;
- : float list = [0.5; 0.499999999999999889; 0.5]
</lang>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 20:14, 24 February 2009

Task
First-class functions
You are encouraged to solve this task according to the task description, using any language you may know.

According to wp:First-class functions, a language has first class functions if:

  • New functions can be created from others at run time
  • Functions can be stored in other collection types of the language
  • Functions can be used as arguments to other functions
  • Functions can be returned as the value of functions.

The above to be accomplished without invoking a compiler or eval/exec or metaprogramming from the main program.

Write a program to create an ordered collection of a mixture of built-in and user defined functions of a real number, together with another ordered collection of their inverses. Try and use sin, asin, cos, acos, and cube, cube-root as the functions. Create a function compose, that given two functions as arguments returns a new function that applys first the second argument to compose to its argument, then the the first argument to compose to the result, i.e:

 composed_function = compose(f,g)
 where composed_function(x) == compose(f,g)(x) == f(g(x))

Applying the compose of a function and its inverse from the two ordered collections of functions in pairs, show that the result in each case is the original value.

The Python example conforms to the above.

Haskell

<lang haskell>Prelude> let cube x = x ^ 3 Prelude> let croot x = x ** (1/3) Prelude> let compose f g = \x -> f (g x) -- this is already implemented in Haskell as the "." operator Prelude> -- we could have written "let compose f g x = f (g x)" but we show this for clarity Prelude> let funclist = [sin, cos, cube] Prelude> let funclisti = [asin, acos, croot] Prelude> zipWith (\f inversef -> (compose inversef f) 0.5) funclist funclisti [0.5,0.4999999999999999,0.5] </lang>

OCaml

<lang ocaml># let cube x = x ** 3.;; val cube : float -> float = <fun>

  1. let croot x = x ** (1. /. 3.);;

val croot : float -> float = <fun>

  1. let compose f g = fun x -> f (g x);; (* we could have written "let compose f g x = f (g x)" but we show this for clarity *)

val compose : ('a -> 'b) -> ('c -> 'a) -> 'c -> 'b = <fun>

  1. let funclist = [sin; cos; cube];;

val funclist : (float -> float) list = [<fun>; <fun>; <fun>]

  1. let funclisti = [asin; acos; croot];;

val funclisti : (float -> float) list = [<fun>; <fun>; <fun>]

  1. List.map2 (fun f inversef -> (compose inversef f) 0.5) funclist funclisti;;

- : float list = [0.5; 0.499999999999999889; 0.5] </lang>

Python

Python 2.X <lang python>>>> #some built in functions and their inverses >>> from math import sin, cos, acos, asin >>> # Add a user defined function and its inverse >>> cube = lambda x: x * x * x >>> croot = lambda x: x ** (1/3.0) >>> # First class functions allow run-time creation of functions from functions >>> # return function compose(f,g)(x) == f(g(x)) >>> compose = lambda f1, f2: ( lambda x: f1(f2(x)) ) >>> # first class functions should be able to be members of collection types >>> funclist = [sin, cos, cube] >>> funclisti = [asin, acos, croot] >>> # Apply functions from lists as easily as integers >>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)] [0.5, 0.49999999999999989, 0.5] >>> </lang>