yours:
> (get-index 6 '(a b (c d e) f a))
(0)
mine:
> (get-index 6 '(a b (c d e) f a))
(4)
I wanted get flat length to return 1 on atoms, so it's more like:
(define (get-flat-length lst)
(if (list? lst) (length (flat lst)) 1))
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
(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))))
(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)))
(define (prit)
(print "printed!"))
(define (ret-nil)
'())
(define-macro (my-or)
(let (val (eval (args 0)))
(if val val (apply my-or (rest (map eval (args)))))))
(define (my-or2 p)
(if p p (if (args) (apply my-or2 (args)) p)))
(define-macro (my-or3)
(if (empty? (args))
nil
(if (or (eval (args 0)) (= 1 (length (args))))
(eval (args 0))
(eval (cons 'my-or3 (rest (args)))))))
(define (fun-my-or2 )
(let (val (eval (args 0)))
(if val
val
(apply fun-my-or2 (rest (args))))))
(define-macro (my-or4 )
(apply fun-my-or2 (args)))
> (my-or nil (ret-nil) (cons 2 3) (prit))
printed!(2 3)
> (my-or2 nil (ret-nil) (cons 2 3) (prit))
printed!(2 3)
> (my-or3 nil (ret-nil) (cons 2 3) (prit))
(2 3)
> (my-or4 nil (ret-nil) (cons 2 3) (prit))
(2 3)
>
(set 'lst '(1 2 3))
(let (a 3)
(mydolist '(e lst)
'(+ a 3)))
(let (a 3)
(eval (mydolist '(e lst) '(+ a 3))))
(define (negate fun)
(letex (fun fun)
(fn () (not (apply fun (args))))))
> (map number? '(1 2 3 "hi"))
(true true true nil)
> (map (negate number?) '( 1 2 3 "lol"))
(nil nil nil true)
(collector
(dolist (e lst)
(if (and (number? e) (> e 1))
(build (+ 1 e)))))
> [cmd]
(collector
(for (e 1 10)
(if (> e 3)
(build (+ 1 e)))))
[/cmd]
(5 6 7 8 9 10 11)
> (builder * 1 (for (i 1 4) (build i)))
24
(define-macro (collector)
(let (collector-list '())
(eval
(cons 'begin
(expand (args)
'((build
'(lambda (element)
(push element collector-list -1)))))))
collector-list))
(define (custom-builder)
(expand
'(let (building basecase)
(eval
(cons 'begin
(expand (args)
'((build
(fn (element)
(build-function element 'building)))))))
building)
(list (list 'build-function (args 0)) (list 'basecase (args 1)))))
(define-macro (builder fun base)
(letex (fun fun base base)
(eval (custom-builder
(fn (num total) (set total (fun num (eval total))))
base))))