Hacker News new | past | comments | ask | show | jobs | submit login

Ah, but how would you easily access the number 6 out of the nested list '((((5 4 3 2 1) 6) 7) 8 9) without one of the cute compositions of "car" and "cdr"? In this case, "cadaar". (Rhetorical question. "car" and "cdr" are neat for their cute composition, but I would never inflict that on someone first learning the language.)



    (def x ((((5 4 3 2 1) 6) 7) 8 9))

    (get-in x (0 0 0 1))
Probably botched quoting, don't know common lisp, only clojure.


For the heck of it, I though I'd try it in scheme with pattern matching...

  (let ((x '((((5 4 3 2 1) 6) 7) 8 9)))
    (syntax->datum
     (syntax-case x ()
       ((((_ p) _) _ ...) #'p))))


One word: lenses.


And how would that look for this particular example?


Definitely not as terse but far more general, flexible and powerful. With car and cdr you're restricted to working on cons cells while lenses are generic.

Some examples (from Haskell):

    >>> ((((5,4,3,2,1),6),7),8,9) & view (_1 . _1 . _2)
    6
    >>> ((((5,4,3,2,1),6),7),8,9) & (_1 . _1 . _2) .~ 4
    ((((5,4,3,2,1),4),7),8,9)
    >>> let foo = ((((5,4,3,2,1),6),7),8,9)
    >>> foo & view (_1 . _1 . _2)
    6
    >>> let six = _1 . _1 . _2
    >>> foo & view six
    6
    >>> foo & six .~ 4
    ((((5,4,3,2,1),4),7),8,9)
    >>> foo & six .~ "abc"
    ((((5,4,3,2,1),"abc"),7),8,9)




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

Search: