Take this:
Code: Select all
(set 'l:l (sequence 0 1000))
(define (reverse-list lst)
(sort lst <)
(nth-set 0 lst "first"))
(reverse-list l:l)
Code: Select all
(set 'l:l (sequence 0 1000))
(define (reverse-list lst)
(sort lst <)
(nth-set 0 lst "first"))
(reverse-list l:l)
Code: Select all
(set 'l:l (sequence 1 10))
(define (reverse-list lst)
(sort lst:l >)
(nth-set 0 lst:l "first"))
(reverse-list l) ; passed by reference
l:l => ("first" 9 8 7 6 5 4 3 2 1)
Code: Select all
(define (reverse-list lst)
(sort lst:l >)
(nth-set (lst 0) "first"))
(reverse-list l) ; passed by reference
Code: Select all
(sort lst:l >)
Code: Select all
(define-macro (reverse-list)
(sort (eval (args 0)) >) (nth-set 0 (eval (args 0)) "first"))
(set 'l (sequence 1 10))
(reverse-list l)
l => ("first" 9 8 7 6 5 4 3 2 1)
No, it does not have to be the the same name as the context. The default funtor is only important when when you have a syntax like this: (<functor> x y z) in implicit indexing, or (<number> [<number>]<functor>) in implicit rest/slice when <functor> can be the context name alone.to pass by reference, you have to store your data in a context in a symbol with the same name as the context ...
... the value yes, but not the reference.Is there not an easier way to get the value of a default context symbol/functor...?
Code: Select all
(set 'ctx:data (sequence 0 10000))
(define (reverse-list ctxt symbl)
(sort (context ctxt symbl) >)
(nth-set ((context ctxt symbl) 0) (rand 10)))
(define (p-ctxt ctxt)
; just a utility function
(dotree (i ctxt)
(println (eval i))))
(p-ctxt ctx)
(reverse-list ctx "data") ; passing 'ctx:data' by reference?
... that is exactly the point. Also the the same design decisions in newLISP which make it a mostly value-passing language are the design features which also make it small and fast. Big list objects where speed will be affected are rare enough to handle them as a global variable or take the extra step of putting a namespace/context wrapper around it.didn't spot much of a speed benefit, because I rarely used lists large enough... to slow newLISP down...
Code: Select all
(set 'foo:foo '(a b c d e f g))
(define (mysort ctx func)
(sort (eval (ctx)) func))
(mysort foo)
Code: Select all
(sort c:foo func)
Code: Select all
; example with normal symbol
(set 'lst '(4 3 6 5 4 3 7))
(sort lst) => (3 3 4 4 5 6 7)
(set 'ctx 'lst) => assigns the symbol
(symbol? ctx) => true
(sort ctx) => list expected in function sort : 'lst
(sort (eval ctx)) => (3 3 4 4 5 6 7)
; example with default symbol
(set 'foo:foo '(4 3 6 5 4 3 7))
(sort foo:foo (3 3 4 4 5 6 7))
(set 'ctx foo) => assigns the context
(symbol? (ctx)) =>true
(sort (ctx)) => list expected in function sort : (ctx)
(sort (eval (ctx))) => (a b c d e f g)
Code: Select all
(set 'foo:foo 123)
(set 'ctx foo)
(set (ctx) 999)
foo:foo => 999