how to return an association list

Started by cormullion, November 03, 2006, 06:42:36 AM

Previous topic - Next topic

Lutz

#15
QuoteEase of expression is more the thing ...  that doesn't require another piece of program code to achieve.


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.



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

lisp

#16
Quote from: "Lutz"
QuoteEase of expression is more the thing ...  that doesn't require another piece of program code to achieve.


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.



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


Quote book material.

rickyboy

#17
Yet another:
(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))

To understand zip's meaning, think about what happens to two separate rows of teeth as they pass through a zipper.



Cheers, --Rick
(λx. x x) (λx. x x)

cormullion

#18
That's neat! And inline:


(transpose (list '(a b c sum) (list a b c sum)))

works for me, and is shorter than:


(list (list "a" a) (list "b" b) (list "c" c) (list "sum" sum))

thanks for all the ideas!

Lutz

#19
Very nice, rickyboy ... and works for n-way zips too:


(zip '(1 2 3) '(a b c) '(x y z)) => ((1 a x) (2 b y) (3 c z))

I will put this into the http://newlisp.org/index.cgi?page=Code_Snippets">http://newlisp.org/index.cgi?page=Code_Snippets page



Lutz

rickyboy

#20
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">//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
(λx. x x) (λx. x x)

nigelbrown

#21
For an unspecified number of arguments:



(define-macro (test )

  (let ((s (map (lambda (x) (list (sym x) (eval x))) (args))))

   (append s (list (list 'sum (apply '+ (map (lambda (x) (x 1)) s)))))))



Line 2 gathers the arguments into an assoc list

Line 3 sums the second elements and sticks it on the end

the local variable s is used so arguments are only evaluated once

so



> (setq a 1)

1

> (setq b 2)

2

> (setq c 3)

3

> (test a b c)

((a 1) (b 2) (c 3) (sum 6))

>



Nigel

PS to keep the original intent of strings in the list use name:



(define-macro (test )

  (let ((s (map (lambda (x) (list (name x) (eval x))) (args))))

   (append s (list (list "sum" (apply '+ (map (lambda (x) (x 1)) s)))))))



gives



> (test a b c)

(("a" 1) ("b" 2) ("c" 3) ("sum" 6))

>

Fanda

#22
Quote from: "rickyboy"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">//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


Rick, I am glad to see that you read my code :-)



Just to be clear on who gets the credit: I first saw zip-merge-dispose from Nigel Brown and named it 'merge':

http://www.alh.net/newlisp/phpbb/viewtopic.php?p=4582">//http://www.alh.net/newlisp/phpbb/viewtopic.php?p=4582


Quote
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


So, credit goes to Nigel!



Fanda



PS: Code Snippets show old update time: last updated 2005-12-08

lisp

#23
Quote from: "Fanda"So, credit goes to Nigel!


I call it, if Nigel doesn't want it.

nigelbrown

#24
I'd completely forgotten dispose.

You can see from my contribution re test above I like using (args)



Nigel