newLISP Fan Club

Forum => newLISP newS => Topic started by: cormullion on August 19, 2008, 01:51:32 PM

Title: is this passing by reference?
Post by: cormullion on August 19, 2008, 01:51:32 PM
Is this an example of passing by reference, or am I doing something else....


(set 'long-text (parse (read-file "/Users/me/long-book.txt") {W} 0))

(define C:C long-text)
(define D long-text)

(define (my-process lst)
  (nth-set (lst 0) "word"))

(time (my-process C) 20)
;-> 0
(time (my-process D) 20)
;-> 497
Title:
Post by: Lutz on August 19, 2008, 02:37:16 PM
You are doing it right, and you see that on a bigger data object reference passing is much faster.



But speed is not the only reason you would do reference passing. The second reason is, that with reference passing you can make 'my-process' a destructive function changing the original contents:


(set 'long-text (parse "this is a sentence") )

(define C:C long-text)
(define D long-text)

(define (my-process lst)
  (nth-set (lst 0) "word "))

(my-process C)

C:C => ("word " "is" "a" "sentence")
 
(my-process D)

D = ("this" "is" "a" "sentence")


C:C has changed but D did not.
Title:
Post by: cormullion on August 20, 2008, 08:36:11 AM
Thanks!



What would you call C, in that example? It's not exactly the default function... Default symbol?
Title:
Post by: Lutz on August 20, 2008, 09:49:24 AM
C is the context or namespace, or is the identifier of the context or namespace.



C:C would be the default symbol.



A context or namespace C,  when used as if a string or list in a function defaults to the default symbol containing the list or string.



When C is used in the functor or operator position of an expression as in (C ...), then C:C is also called the default functor. If C:C contains a function it can also be called the default function, if C:C contains nil and is in the operator/functor position it will work like a hash function.
Title:
Post by: cormullion on August 20, 2008, 10:06:10 AM
Ok, thanks. As usual, I'm looking for the simplest phrase that does the job properly. (Rewriting the whole chapter on Contexts, and it isn't going well...:)