First-class functions/Use numbers analogously: Difference between revisions

From Rosetta Code
Content added Content deleted
(added Ursala)
(CL example)
Line 21: Line 21:


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

=={{header|Common Lisp}}==

<lang lisp>(defun multiplier (x y)
#'(lambda (z) (* x y z)))

(defun first-class-numbers ()
(let* ((x 2.0)
(xi 0.5)
(y 4.0)
(yi 0.25)
(z (+ x y))
(zi (/ 1.0 (+ x y)))
(left (list x y z))
(right (list xi yi zi)))
(loop for l in left
for r in right
do (format t "~&(funcall (multiplier ~w ~w) 0.5) = ~w"
l r (funcall (multiplier l r) 0.5)))))</lang>

<pre>> (first-class-numbers)
(funcall (multiplier 2.0 0.5) 0.5) = 0.5
(funcall (multiplier 4.0 0.25) 0.5) = 0.5
(funcall (multiplier 6.0 0.16666667) 0.5) = 0.5</pre>


=={{header|E}}==
=={{header|E}}==

Revision as of 21:00, 10 August 2009

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

Common Lisp

<lang lisp>(defun multiplier (x y)

 #'(lambda (z) (* x y z)))

(defun first-class-numbers ()

 (let* ((x 2.0)
        (xi 0.5)
        (y 4.0)
        (yi 0.25)
        (z (+ x y))
        (zi (/ 1.0 (+ x y)))
        (left (list x y z))
        (right (list xi yi zi)))
   (loop for l in left
         for r in right
         do (format t "~&(funcall (multiplier ~w ~w) 0.5) = ~w"
                    l r (funcall (multiplier l r) 0.5)))))</lang>
> (first-class-numbers)
(funcall (multiplier 2.0 0.5) 0.5) = 0.5
(funcall (multiplier 4.0 0.25) 0.5) = 0.5
(funcall (multiplier 6.0 0.16666667) 0.5) = 0.5

E

This is written to have identical structure to First-class functions#E, though the variable names are different.

<lang e>def x := 2.0 def xi := 0.5 def y := 4.0 def yi := 0.25 def z := x + y def zi := 1.0 / (x + y) def forward := [x, y, z ] def reverse := [xi, yi, zi]

def multiplier(a, b) {

   return fn x { a * b * x }

}

def s := 0.5 for i => a in forward {

   def b := reverse[i]
   println(`s = $s, a = $a, b = $b, multiplier($a, $b)($s) = ${multiplier(a, b)(s)}`)

}</lang>

Output:

s = 0.5, a = 2.0, b = 0.5, multiplier(2.0, 0.5)(0.5) = 0.5
s = 0.5, a = 4.0, b = 0.25, multiplier(4.0, 0.25)(0.5) = 0.5
s = 0.5, a = 6.0, b = 0.16666666666666666, multiplier(6.0, 0.16666666666666666)(0.5) = 0.5

Note: def g := reverse[i] is needed here because E as yet has no defined protocol for iterating over collections in parallel. Page for this issue.

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.

Ruby

<lang ruby>multiplier = proc {|n1, n2| proc {|m| n1 * n2 * m}} numlist = [x=2, y=4, x+y] numlisti = [0.5, 0.25, 1.0/(x+y)] p numlist.zip(numlisti).map {|n,ni| multiplier.call(n,ni).call(0.5)}

  1. => [0.5, 0.5, 0.5]</lang>

This structure is identical to the treatment of Ruby's first class functions -- create a Proc object that returns a Proc object (a closure). We show that a number (or function) multiplied by its inverse (applied to its inverse function) multiplied by some number (passed some number as an argument) results in that number.

Tcl

Works with: Tcl version 8.5

<lang tcl>package require Tcl 8.5 proc multiplier {a b} {

   list apply {{ab m} {expr {$ab*$m}}} [expr {$a*$b}]

}</lang> Note that, as with Tcl's solution for First-class functions, the resulting term must be expanded on application. For example, study this interactive session: <lang tcl>% set mult23 [multiplier 2 3] apply {{ab m} {expr {$ab*$m}}} 6 % {*}$mult23 5 30</lang> Formally, for the task: <lang tcl>set x 2.0 set xi 0.5 set y 4.0 set yi 0.25 set z [expr {$x + $y}] set zi [expr {1.0 / ( $x + $y )}] set numlist [list $x $y $z] set numlisti [list $xi $yi $zi] foreach a $numlist b $numlisti {

   puts [format "%g * %g * 0.5 = %g" $a $b [{*}[multiplier $a $b] 0.5]]

}</lang> Which produces this output:

2 * 0.5 * 0.5 = 0.5
4 * 0.25 * 0.5 = 0.5
6 * 0.166667 * 0.5 = 0.5

Ursala

The form is very similar to the first class functions task solution in Ursala, except that the multiplier function takes the place of the composition operator (+), and is named in compliance with the task specification. <lang Ursala>

  1. import std
  2. import flo

numbers = <2.,4.,plus(2.,4.)> inverses = <0.5,0.25,div(1.,plus(2.,4.))>

multiplier = //times+ times

  1. cast %eL

main = (gang multiplier*p\<x,y,z> <xi,yi,zi>) 0.5 </lang> The multiplier could have been written in pattern matching form like this. <lang Ursala> multiplier("a","b") "c" = times(times("a","b"),"c") </lang> The main program might also have been written with an anonymous function like this. <lang Ursala> main = (gang (//times+ times)*p\<x,y,z> <xi,yi,zi>) 0.5 </lang> output:

<5.000000e-01,5.000000e-01,5.000000e-01>