de-structuring in newLISP

For the Compleat Fan
Locked
Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

de-structuring in newLISP

Post by Lutz »

Some blogger was showing de-structuring in Clojure (a Java based LISP). Just wanted to share how easy this is in newLISP using 'unify' and 'bind':

Code: Select all

(set 'struct '((one "two") 3 (four ("five"))))
(set 'pattern '((A B) C (D (E))))

; de-structure
(bind (unify pattern struct))

A → one
B → "two"
C → 3
D → four
E → "five"


'unify' returns an association list and 'bind' binds the associations

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I don't get that - what's de-structuring for? (no entry in wikipedia...). But I like to see unify being used - I've yet to use it in my stuff!

(PS: I think Fanda is a Clojure user now...- saw his name crop up on a Clojure mailing list...)

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post by newBert »

Some things in CloJure can be done simply (more simply) in NewLISP

e.g.:

Code: Select all

;;; CloJure ;;;
; (defn my-zipmap [keys vals]
;	(loop 
;		[map {}
;		 ks (seq keys)
;		 vs (seq vals)]
;		(if (and ks vs)
;			(recur (assoc map (first ks) (first vs))
;				(rest ks)
;				(rest vs))
;			map)))
;
; (my-zipmap [:a :b :c] [1 2 3])			
; => (:b 2, :c 3, :a 1)

Code: Select all

;;; NewLISP ;;;
(println (map list '(a b c) '(1 2 3)))
; => ((a 1) (b 2) (c 3))
... or maybe I did not undestand at all ?

P.S.:
cormullion wrote:I don't get that - what's de-structuring for?
neither do I
Image
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Post by xytroxon »

I think it's from this blog: "Some notes about Clojure"
http://items.sjbach.com/16/some-notes-about-clojure

reddit programming: comment link.

If you understand it enough, you can taunt er. "comment" about newLISP's version at both of above links ;)

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

xytroxon wrote:If you understand it enough, you can taunt er. "comment" about newLISP's version at both of above links ;)
;) always tempting! But then I liked what Rich Hickey (Mr Clojure) said in a comment on another blog somewhere - if you want others to respect your language, then respect their's too...

I like design of the clojure web site (clojure.org) as well. (Lutz - perhaps it's time to retire all those HTML CENTER tags on newlisp.org?)

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post by newBert »

Thanks for the links, I understand a little better now what it is about ...

... and I find Lutz's example (in NewLISP) clearer and more readable (and more "Lispic") than those in CloJure.

CloJure

Code: Select all

(def flat "flat")
(def tree '(("one" ("two")) "three" ((("four")))))
 
;; Simple binding (like Common Lisp's LET*).
(let [var1 flat
      var2 tree]
  (list var1 var2))
 
-> ("flat" (("one" ("two")) "three" ((("four")))))
 
;; Full destructuring.
(let [var1 flat
      [[a [b]] c [[[d]]]] tree]
  (list var1 a b c d))
 
-> ("flat" "one" "two" "three" "four")
NewLISP

Code: Select all

(set 'flatten "flat")
(set 'tree '(("one" ("two")) "three" ((("four")))))

;; Simple binding (like Common Lisp's LET*)
(let (var1 flatten
			 var2 tree)
	(list var1 var2))

;-> ("flat" ("one" ("two")) "three" ((("four"))))

;; Full destructuring.
(let (var1 flatten)
  (bind (unify '((A (B)) C (((D)))) tree))
  (list var1 A B C D))

;-> ("flat" "one" "two" "three" "four")
N.B.:(flat (list flatten tree)) seems simpler for full destructuring ;-)

That gave rise to play with unify and above all to understand it.
(And I did not finish understanding yet)
;-)
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

xytroxon
Posts: 296
Joined: Tue Nov 06, 2007 3:59 pm
Contact:

Post by xytroxon »

cormullion wrote:
xytroxon wrote:If you understand it enough, you can taunt er. "comment" about newLISP's version at both of above links ;)
;) always tempting! But then I liked what Rich Hickey (Mr Clojure) said in a comment on another blog somewhere - if you want others to respect your language, then respect their's too...
That's why I said you need to understand it first!

Clojure has a lot of "Why did they want or have to do it that way?" changes.

Differences with other Lisps:
http://clojure.org/lisps

So it is interesting to study... But hard to challenge the language without a lot more experience using Clojure.
cormullion wrote: I like design of the clojure web site (clojure.org) as well. (Lutz - perhaps it's time to retire all those HTML CENTER tags on newlisp.org?)
For looking like it doesn't need to use javascript, it has too much javascript for my taste ;)

-- xytroxon
"Many computers can print only capital letters, so we shall not use lowercase letters."
-- Let's Talk Lisp (c) 1976

newBert
Posts: 156
Joined: Fri Oct 28, 2005 5:33 pm
Location: France

Post by newBert »

cormullion wrote: ;) always tempting! But then I liked what Rich Hickey (Mr Clojure) said in a comment on another blog somewhere - if you want others to respect your language, then respect their's too...
Comparing can be also a token of respect, because of the interest that we take to learn and understand another language ... :-)
BertrandnewLISP v.10.7.6 64-bit on Linux (Linux Mint 20.1)

Locked