Andrew Gwozdziewycz on 30 Jan 2008 14:14:30 -0800 |
On Jan 30, 2008 3:55 PM, Ed Watkeys <edw@poseur.com> wrote: > > There are a couple things that I want to see idiomatic code for. Most > importantly is looping: In Scheme, you could write a procedure in to > compute the Nth fib thusly: > > (define (fib n) > (let loop ((a 0) (b 1) (n n)) > (if (zero? n) > a > (loop b (+ a b) (- n 1))))) > > But in Arc? There's no named-let, no letrec, so I came up with this: > > (def fib (n) > (with (a 0 b 1) > (for i 1 n > (let old-a a > (= a b) > (= b (+ old-a b)))) > a)) > > What a mess! Does Arc have nested definitions? Let me check... > > > (def fib (n) > (def iter (n a b) > (if (is n 0) > a > (iter (-- n) b (+ a b)))) > (iter n 0 1)) > > Yes, that works, sort of: ITER gets defined in the global scope. So a > separate helper function is called for... > > (def fib-helper (n a b) > (if (is n 0) a (fib-helper (-- n) b (+ a b)))) > > (def fib (n) > (fib-helper (n 0 1))) > > That first version also does a lot of mutation. Hmm... > > Andrew, regarding your comment on the nature of the code, I hear you, > but I like to take the plunge into a language and try to appreciate it > on it's own terms for a while and see how things go. It could wind up > sucking, but there's enough interesting stuff to merit swishing the > Kool-Aid around in my mouth for a bit. > I'm not saying that I won't peck around with it... (I've already been procrastinating at work to do so) but, I don't yet see anything that makes my mouth drop and say, "duh, why haven't I been doing it this way all along!" arc> (= x 1) 1 arc> (mac foo (y) (+ y x)) *** redefining foo #3(tagged mac #<procedure>) arc> (foo 2) 3 arc> (with (x 99) (foo 2)) 3 arc> arc> ^Cuser break > (define x 1) > (define-syntax foo (syntax-rules () ((_ y) (+ x y)))) > (foo 2) 3 > (let ((x 99)) (foo 2)) 3 > > (require (lib "defmacro.ss")) > (define-macro (foo y) `(+ x ,y)) > (foo 2) 3 > (let ((x 99)) (foo 2)) 101 > -- Andrew Gwozdziewycz apgwoz@gmail.com http://www.apgwoz.com | http://www.photub.com
|
|