4 ms·
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 t
by beeworker 12y ago
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.)
- ajuc 12y ago(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.
- chongli 12y agoOne word: lenses.
- Flow 12y agoAnd how would that look for this particular example?
- chongli 12y agoDefinitely 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)
- GregBuchholz 12y agoFor 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))))