Hacker News new | past | comments | ask | show | jobs | submit login
Struquine: A Useful Lisp Trick (dorophone.blogspot.com)
31 points by soundsop on Aug 18, 2008 | hide | past | favorite | 2 comments



Here is an arc version of the struquine macro:

  (mac strq (name . slots) 
     (withs (bname 
             (coerce (string name "#") 'sym) 
             creator `((def ,(coerce (string name "?") 'sym) (thing) (is (car thing) ',bname))
                       (def ,bname ,slots (cons ',bname (list ,@slots))))
             accessors nil)
            (on arg slots
                (= accessors (cons `(def ,(coerce (string name "-" arg) 'sym) (thing) (thing ,(+ index 1))) accessors)))
            (cons 'do (join accessors creator))))
I had to use "#" instead of "!" since that is used for special function access in arc, but the effect of this code is pretty much what was written about in the blog post. It creates a constructor for the struquine with name#, accessors for each arg of the form name-arg, and "type" checker of the form name?. For example:

  arc> (strq point x y)
  #<procedure: point#>
  arc> (= mypoint (point# 1 2))
  (point# 1 2)
  arc> (point? mypoint)
  t
  arc> (point-x mypoint)
  1
  arc> (point-y mypoint)
  2


As I use other languages more and more, I begin to realize just how powerful lisp's simple "everything is a list" paradigm is.

For example, one homework was to create an object oriented forum. The thing was so complex because I was supposed to differentiate between forum, topic, thread, and post. However, if I made a post a cons cell, then it all collapsed into a branching list where everything is a post, which also gave me easy branching threads.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: