Code: Select all
(define (test)
(local (a b c)
(set 'a 1)
(set 'b 2)
(set 'c 3)
(set 'sum (+ a b c))
'(("a" a) ("b" b) ("c" c) ("sum" sum))
))
(println (lookup "sum" (test)))
Code: Select all
(define (test)
(local (a b c)
(set 'a 1)
(set 'b 2)
(set 'c 3)
(set 'sum (+ a b c))
'(("a" a) ("b" b) ("c" c) ("sum" sum))
))
(println (lookup "sum" (test)))
Code: Select all
(define (test)
(letex (a 1 b 2 c 3)
(letex (sum (+ a b c))
'(("a" a) ("b" b) ("c" c) ("sum" sum)) )
) )
(test) -> (("a" 1) ("b" 2) ("c" 3) ("sum" 6))
Code: Select all
(define (test)
(local (a b c)
(set 'a 1)
(set 'b 2)
(set 'c 3)
(set 'sum (+ a b c))
(expand (list a b c))))
Code: Select all
(define (test)
(letn (a 1 b 2 c 3 sum (+ a b c))
(expand '(("a" a) ("b" b) ("c" c) ("sum" sum)) 'a 'b 'c 'sum)))
(test) -> (("a" 1) ("b" 2) ("c" 3) ("sum" 6))
Code: Select all
(define (test)
(letex (a 1 b 2 c 3)
(letex (sum (+ a b c))
'(("a" a) ("b" b) ("c" c) ("sum" sum)) )
) )
(test) -> (("a" 1) ("b" 2) ("c" 3) ("sum" 6))
Code: Select all
(define (test)
(letexn (a 1 b 2 c 3 sum (+ a b c))
'(("a" a) ("b" b) ("c" c) ("sum" sum)) ))
Code: Select all
(define (test)
(letn (a 1 b 2 c 3 sum (+ a b c))
(list (list "a" a) (list "b" b) (list "c" c) (list "sum" sum))))
Code: Select all
(define (test)
(local (a b c sum)
(set 'a 1 'b 2 'c 3 'sum (+ a b c))
(list (list "a" a) (list "b" b) (list "c" c) (list "sum" sum))))
(println (lookup "sum" (test)))
Code: Select all
(constant (global 'pair) (fn (lst)
(array-list (array (/ (length lst) 2) 2 lst))))
Code: Select all
(define (test)
(local (a b c sum)
(set 'a 1 'b 2 'c 3 'sum (+ a b c))
(pair (list "a" a "b" b "c" c "sum" sum))))
Code: Select all
(define (group lst (n 2) (keep-tail true))
(set 'lst (map (lambda (i) (slice lst i n)) (sequence 0 (- (length lst) 1) n)))
(if (and (not keep-tail) (!= n (length (last lst))))
(pop lst -1))
lst)
Code: Select all
(array 3 2) => ((nil nil) (nil nil) (nil nil))
; or with initialization
(array 3 2 '(1 2 3 4 5 6)) => ((1 2) (3 4) (5 6))
Code: Select all
(define (group lst (n 2))
(array-list (array (/ (length lst) n) n lst)))
I wholeheartedly agree. Ease of expression and self sufficiency should always be the first priority, before efficiency considerations, when coding. Decisions in this area always have a subjective component, which is fine and how it should be.Ease of expression is more the thing ... that doesn't require another piece of program code to achieve.
Quote book material.Lutz wrote:I wholeheartedly agree. Ease of expression and self sufficiency should always be the first priority, before efficiency considerations, when coding. Decisions in this area always have a subjective component, which is fine and how it should be.Ease of expression is more the thing ... that doesn't require another piece of program code to achieve.
Because of this I also reject 'standardized coding styles' or the notion that there is a 'right way' of doing things.
Much of the fun in programming stems from the freedom we can take to express (code) in our own individual way and a prgrammming language should allow this.
The best programmers are those who take the freedom to cultivate their own styles.
Lutz
Code: Select all
(define (zip) (transpose (args)))
(define (test)
(local (a b c sum)
(set 'a 1)
(set 'b 2)
(set 'c 3)
(set 'sum (+ a b c))
(zip '(a b c sum) (list a b c sum))))
> (test)
((a 1) (b 2) (c 3) (sum 6))
Code: Select all
(transpose (list '(a b c sum) (list a b c sum)))
Code: Select all
(list (list "a" a) (list "b" b) (list "c" c) (list "sum" sum))
Code: Select all
(zip '(1 2 3) '(a b c) '(x y z)) => ((1 a x) (2 b y) (3 c z))
Rick, I am glad to see that you read my code :-)rickyboy wrote:Oops! You had better not give me credit for that. The idea and name of 'zip' is something taken from FP languages like Haskell or ML. The newlisp implementation of 'zip' I first saw in Fanda's function 'merge' in http://www.intricatevisions.com/source/newlisp/list.lsp. When I saw Fanda's 'merge', I thought "Hey that's zip and a very pithy definition at that." So, I vote that you strike my name from the Snippets credit and stick Fanda's name there. :-) Cheers, --Rick
So, credit goes to Nigel!Here is a dispose that is just a wrapper around the builtin newLISP function transpose.
using args lets you accept any number of lists
viz
> (define (dispose) (transpose (args)))
(lambda () (transpose (args)))
> (dispose '(1 2 3) '(4 5 6))
((1 4) (2 5) (3 6))
> (dispose '(1 2 3) '(4 5 6) '(7 8 9))
((1 4 7) (2 5 8) (3 6 9))
> (setq m '(1 2 3 4 5 6 7))
(1 2 3 4 5 6 7)
> (setq n '(7 6 5 4 3 2 1))
(7 6 5 4 3 2 1)
> (dispose m n)
((1 7) (2 6) (3 5) (4 4) (5 3) (6 2) (7 1))
>
Nigel