Code: Select all
(set 'lst (list (list 1 2 3) (list 'a 'b 'c)))
(set 'a-ref (ref 'a lst))
; a-ref is (1 0)
Code: Select all
(lst a-ref)
;-> a
Code: Select all
(nth '''a-ref?''' lst)
Code: Select all
(nth (first a-ref) (last a-ref) lst)
;-> a
Code: Select all
(set 'lst (list (list 1 2 3) (list 'a 'b 'c)))
(set 'a-ref (ref 'a lst))
; a-ref is (1 0)
Code: Select all
(lst a-ref)
;-> a
Code: Select all
(nth '''a-ref?''' lst)
Code: Select all
(nth (first a-ref) (last a-ref) lst)
;-> a
Code: Select all
(set 'lst (list (list 1 2 3) (list 'a 'b 'c)))
Code: Select all
(set 'lst '((1 2 3) (a b c)))
Code: Select all
(nth (first a-ref) (last a-ref) lst)
Code: Select all
(set 'L '((1 2 3) (a b c)))
(set 'aref (ref 'c L)) => (1 2)
; new for nth
(nth (L 1 2)) => c
(nth (L aref)) => c
; new for set-nth, nth-set
(set-nth (L aref) 99) => ((1 2 3) (a b 99))
(nth-set (L aref) 99) => c
Cool! Great work, thanks! Nearly as cool as (nth L aref) or (nth aref L) :-)!Lutz wrote:These are the additional syntax forms, all old forms still work:
I hope this is right at the bottom of your to-do list! I don't want to have to go through all my old code and use less readable syntax. More readable, I could cope with, perhaps! :-)Lutz wrote:ps: I am still thinking of eliminating the completely flat forms sometimes in the future.
Code: Select all
(set 'f '((a b d (1 2 3 ( x y z)))))
(f 1 2 3)
;-> d
(define (f x y z)
(println (+ x y z)))
(f 1 2 3)
;-> 6
If it happens at all, it would follow these steps:I hope this is right at the bottom of your to-do list!
Code: Select all
(setq x '( ( a b c d e ) ( 1 2 3 4 5 )))
(nth-set 0 2 x "B") => c
; or
(nth-set (x 0 2) "B") => c
; or starting in 9.0.6
(set 'aref (ref 'c x)) => (0 2)
(nth-set (x aref) "B") =>
But that's the same asLutz wrote:Code: Select all
; new for nth (nth (L 1 2)) => c (nth (L aref)) => c
Code: Select all
(L 1 2) => c
(L aref) => c
looks like CL's SETF to me (barring the return values). And actually the following makes more sense to me from a readability standpoint:Lutz wrote:Code: Select all
; new for set-nth, nth-set (set-nth (L aref) 99) => ((1 2 3) (a b 99)) (nth-set (L aref) 99) => c
Code: Select all
(setf (L aref) 99)
Code: Select all
; new for nth
(nth (L 1 2)) => c
(nth (L aref)) => c
Code: Select all
(L 1 2) => c
(L aref) =>
Lutz wrote:(flat x) is non-destructive and returns a new list on which set-nth will work. So you would have to do a (set 'x (flat x)) first.
But I don't understand why you would want to address a nested list with a single index? Then why not have the list flat in the first place?
Lutz
Code: Select all
(define (get-index ind lst , current)
(set 'current 0)
(catch
(dolist (e lst)
(cond
(= current ind)
(throw (list current)
(list? (car e))
(let (len (get-flat-lenght (car e)))
(if (> (- ind current) len)
(set 'current (+ len current))
(throw (cons current (get-index (- ind current) (car e))))))
(set 'current (+ current 1)))))))
(define (get-flat-length lst)
(length (flat lst)))
Code: Select all
(define (get-index idx lst)
(ref ((flat lst) idx) lst))
(get-index 3 '(a b (c d e))) => (2 1)
Code: Select all
(set 'lst (list (list 1 2 3) (list 'a 'b 'c)))
(set 'a-ref (ref 'a lst))
(define-macro (my-nth _aref _l)
(local (_n0 _n1 _n2 _n3 _n4 _n5 _n6 _n7 _n8 _n9)
(set '_n0 (nth 0 (eval _aref)))
(set '_n1 (nth 1 (eval _aref)))
(set '_n2 (nth 2 (eval _aref)))
(set '_n3 (nth 3 (eval _aref)))
(set '_n4 (nth 4 (eval _aref)))
(set '_n5 (nth 5 (eval _aref)))
(set '_n6 (nth 6 (eval _aref)))
(set '_n7 (nth 7 (eval _aref)))
(set '_n8 (nth 8 (eval _aref)))
(set '_n9 (nth 9 (eval _aref)))
(nth _n0 _n1 _n2 _n3 _n4 _n5 _n6 _n7 _n8 _n9 (eval _l))))
(lst a-ref)
;-> a
(my-nth a-ref lst)
;-> a
Code: Select all
(define-macro (my-nth _ref _lst)
(eval (cons 'nth (append (eval _ref) (list _lst)))))
(my-nth a-ref lst) => a
Code: Select all
(define-macro (my-nth)
(eval (cons 'nth (append (eval (args 0)) (list (args 1))))))
(my-nth a-ref lst) => a
Code: Select all
(define (get-index ind lst (current 0))
(if (not (list? lst)) '()
(let (len (get-flat-length (first lst)))
(if (> ind (- len 1))
(get-index (- ind len) (rest lst) (+ 1 current))
(cons current (get-index ind (first lst)))))))
(define (get-flat-length lst)
(cond
((null? lst) 0)
((list? lst) (+ (get-flat-length (first lst)) (get-flat-length (rest lst))))
(true 1)))
(define-macro (flat-nth-set ind lst ele)
(eval (append '(nth-set) (get-index ind (eval lst)) (list lst) (list ele))))
Code: Select all
(define (get-flat-length lst)
(length (flat lst))))
Can you give an example where your defintion of 'get-index' works different from this one?True, but get-index doesn't work with duplicated items.
Code: Select all
(define (get-index idx lst)
(ref ((flat lst) idx) lst))