de-structuring in newLISP

Started by Lutz, October 08, 2008, 07:43:20 AM

Previous topic - Next topic

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':


(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

#1
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

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



e.g.:
;;; 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)


;;; 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.:
Quote from: "cormullion"I don't get that - what's  de-structuring for?

neither do I

http://smileys.sur-la-toile.com/repository/Cligne/b_wink.gif">
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

xytroxon

#3
I think it's from this blog: "Some notes about Clojure"

http://items.sjbach.com/16/some-notes-about-clojure">//http://items.sjbach.com/16/some-notes-about-clojure



http://www.reddit.com/r/programming/comments/75p8f/cool_things_about_clojure_which_havent_gotten">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

#4
Quote from: "xytroxon"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

#5
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
(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
(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)

;-)
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>

xytroxon

#6
Quote from: "cormullion"
Quote from: "xytroxon"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">//http://clojure.org/lisps



So it is interesting to study... But hard to challenge the language without a lot more experience using Clojure.


Quote from: "cormullion"
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

#7
Quote from: "cormullion"
;) 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 ... :-)
<r><I>>Bertrand<e></e></I> − <COLOR color=\"#808080\">><B>newLISP<e></e></B> v.10.7.6 64-bit <B>>on Linux<e></e></B> (<I>>Linux Mint 20.1<e></e></I>)<e></e></COLOR></r>