Anonymous user
First-class functions/Use numbers analogously: Difference between revisions
First-class functions/Use numbers analogously (view source)
Revision as of 01:26, 23 August 2009
, 14 years agoCL code is now closer to the first class functions entry, and both appear for comparison
m (move omits down, omit TI-BASIC) |
(CL code is now closer to the first class functions entry, and both appear for comparison) |
||
Line 23:
=={{header|Common Lisp}}==
#'(lambda (z) (* x y z)))▼
<lang lisp>(defun
#'(
(xi 0.5)▼
(y 4.0)▼
(yi 0.25)▼
(z (+ x y))▼
(zi (/ 1.0 (+ x y)))▼
(left (list x y z))▼
(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>▼
(let* ((x 2.0)
(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>▼
(loop with value = 0.5
for multiplier = (multiplier number inverse)
number
inverse
value
Output:
The code from [[First-class functions]], for comparison:
<lang lisp>(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
(defun cube (x) (expt x 3))
(defun cube-root (x) (expt x (/ 3)))
(loop with value = 0.5
for function in (list #'sin #'cos #'cube )
for inverse in (list #'asin #'acos #'cube-root)
for composed = (compose inverse function)
do (format t "~&(~A ∘ ~A)(~A) = ~A~%"
inverse
function
value
(funcall composed value)))</lang>
Output:
(#<FUNCTION ASIN> ∘ #<FUNCTION SIN>)(0.5) = 0.5
(#<FUNCTION ACOS> ∘ #<FUNCTION COS>)(0.5) = 0.5
(#<FUNCTION CUBE-ROOT> ∘ #<FUNCTION CUBE>)(0.5) = 0.5
=={{header|E}}==
|