Ed Watkeys on 30 Jan 2008 12:55:50 -0800 |
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. Ed On Jan 30, 11:28 am, "Andrew Gwozdziewycz" <apg...@gmail.com> wrote: > On Jan 30, 2008 10:07 AM, Ed Watkeys <e...@poseur.com> wrote: > > > > > > > Here's my cheatsheet so far. Written from a Schemer's perspective. > > With R6RS, I'm looking for something else to fall in love with. PG > > seems to have taken a step back from terseness-uber-alles and has come > > up with something that's pretty nice. > > > ; (DEF funcname (a ...) e ...) > > ; (FN (a ...) e ...) > > ; (IF c1 e1 c2 e2 ... e) -- replaces COND > > ; (WHEN c e1 e2 ...) > > ; (IS a b ...) > > ; (ISO a b ...) > > ; (LET x v e ...) > > ; (WITH (x a y b ...) e ...) > > ; (DO e ...) > > ; (= lval val) > > ; t, nil > > ; ("foo" 0) ==> #\0 > > ; (LEN collection) > > ; (IN x a ...) > > ; (CASE val k1 v1 k2 v2 ... elsev) > > ; (FOR i lo hi e) > > ; (EACH x collection e) > > ; (WHILE c e e1 ...) > > ; (REPEAT n e) > > ; A C-like loop operator... In there somewhere... > > ; [ func _ arg ...] ==> (fn (x) (func x arg ...)) > > ; foo:bar ==> (fn (a ...) (foo (bar a ...))) > > ; ~foo ==> (fn (a ...) (no (foo a ...))) > > ; KEEP filters, REM removes, ALL t if all t, SOME, POS first position > > ; where t, TRUES list of all t values. All work on strings. If func > > arg > > ; is a value, treated as func that tests for equality with the > > value. > > ; (table key) ==> value > > ; (LISTTAB '((k1 v1) (k2 v2) ...)) > > ; MAPTABLE works by side effects, returns table. > > ; (ALREF alist key) ==> value > > ; (STRING v ...) ==> (for-each pr (list v ...)) > > ; (TOSTRING v ...) ==> "..." > > ; (TYPE v) ==> type-of-v > > ; (ISA a type) > > ; (COERCE v type) ==> new-v (types: 'int <radix>, 'cons, ?) > > ; PUSH and POP treat list as stack, work w/ any lvalue. > > ; ++ and -- > > ; (ZAP func x) ==> (= x (func x)) > > ; (SORT cmp collection) -- COMPARE is standard comparator > > ; (INSORT cmp newval collection) > > ; (def func (a (o b)) ...) -- b is optional, nil if not given > > ; (def func (a (o b e) ...) -- b is optional, = to e if not given > > ; Rest params as in Scheme, APPLY as in Scheme. > > ; (MAC macname (a ...) e ...) > > ; Backquoting as in Scheme. > > ; (UNIQ) ==> unique identifier > > ; (W/UNIQ V e ...) -- V can be a symbol or a list of syms > > ; > > ; QUESTIONS: DEFTEM TEMLOAD SAVE-TABLE ERRSAFE TAG ENSURE-DIR > > I wasn't impressed with the terseness of the example code. Maybe it's > my lack of familiarity with it, but the examples were worse to read > than Perl. > > -- > Andrew Gwozdziewycz > apg...@gmail.comhttp://www.apgwoz.com ;|http://www.photub.com
|
|