First-class functions/Use numbers analogously

From Rosetta Code
Revision as of 20:00, 5 August 2009 by rosettacode>Paddy3118 (New task as a comparison for First-class functions, with Python example.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
First-class functions/Use numbers analogously
You are encouraged to solve this task according to the task description, using any language you may know.

In First-class functions, a language is showing how its manipulation of functions is similar to its manipulation of other types.

This tasks aim is to compare and contrast a languages implementation of First class functions, with its normal handling of numbers.


Write a program to create an ordered collection of a mixture of literally typed and expressions producing a real number, together with another ordered collection of their multiplicative inverses. Try and use the following pseudo-code to generate the numbers for the ordered collections:

  x  = 2.0
  xi = 0.5
  y  = 4.0
  yi = 0.25
  z  = x + y
  zi = 1.0 / ( x + y )

Create a function multiplier, that given two numbers as arguments returns a function that when called with one argument, returns the result of multiplying the two arguments to the call to multiplier that created it and the argument in the call:

 new_function = multiplier(n1,n2)
 # where new_function(m) returns the result of n1 * n2 * m

Applying the multiplier of a number and its inverse from the two ordered collections of numbers in pairs, show that the result in each case is one.
Compare and contrast the resultant program with the corresponding entry in First-class functions. They should be close.

To paraphrase the task description: Do what was done before, but with numbers rather than functions


Python

This new task: <lang python> IDLE 2.6.1 >>> # Number literals >>> x,xi, y,yi = 2.0,0.5, 4.0,0.25 >>> # Numbers from calculation >>> z = x + y >>> zi = 1.0 / (x + y) >>> # The multiplier function is similar to 'compose' but with numbers >>> multiplier = lambda n1, n2: (lambda m: n1 * n2 * m) >>> # Numbers as members of collections >>> numlist = [x, y, z] >>> numlisti = [xi, yi, zi] >>> # Apply numbers from list >>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)] [0.5, 0.5, 0.5] >>> </lang>

The Python solution to First-class functions for comparison: <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.4999999999999999, 0.5] >>> </lang> As can be see, the treatment of functions is very close to the treatment of numbers. there are no extra wrappers, or function pointer syntax added, for example.