[linux-l] derivative example in groovy

Ivan F. Villanueva B. iv at artificialidea.com
Di Aug 29 16:22:38 CEST 2006


Am So, Aug 27, 2006 02:44:36 +0200, David Hansen schrieb:
> On Sun, 27 Aug 2006 12:43:21 +0200 Ivan F. Villanueva B. wrote:
> 
> > Ich würde es gerne in Perl, Rubby, Python, Ocalm und D
> > sehen.
> 
> Na dann nochmal scheme:
> 
> (define (square x) (* x x))
> (define (cube x) (* x x x))
> (define functions '(sin cos square cube))
> 
> (let ((dx .0001) (d .1) (x0 .0) (x1 3.))
>   (for-each
>    (lambda (sym)
>      (let* ((f (eval sym (interaction-environment)))
>             (df (lambda (x) (/ (- (f (+ x (/ dx 2))) (f (- x (/ dx 2)))) dx))))
>        (display (string-append (symbol->string sym) ":\t"))
>        (let loop ((x x0))
>          (display (list x (f x) (df x)))
>          (if (> x1 x) (loop (+ x d))))
>        (newline)))
>    functions))
> 
> Wo bleibt der Perl 1-Zeiler?!

Auf den warte ich noch auch. Aber hier etwas kurzer und *Übersichtlicher* (und
treuer zu den anderen Beispielen)

========================================
#!groovy
square = {it * it}
cube = {it * it * it}
functions = ["square":square, "cube":cube, "sin":{Math.sin(it)}, "cos":{Math.cos(it)}]
df = {f,x | (f(x + 0.0001) - f(x)) / 0.0001}
functions.each { name, f ->
    println name
    for (i in 1..64) {
        x = i * 0.01
        println "x: ${x}, y: ${f(x)}, y': ${df(f,x)}"
    }
}
========================================

Ihr habt es gewollt! ;-)
-- 
Ivan F. Villanueva B.
A.I. library:   http://www.artificialidea.com
<<<          The European Patent Litigation Agreement (EPLA)          >>>
<<<            will bring Software patents by the backdoor            >>>
<<<  http://www.no-lobbyists-as-such.com/florian-mueller-blog/epla/   >>>
<<<                     http://wiki.ffii.de/EplaEn                    >>>



Mehr Informationen über die Mailingliste linux-l