Page 1 of 1

is this passing by reference?

Posted: Tue Aug 19, 2008 8:51 pm
by cormullion
Is this an example of passing by reference, or am I doing something else....

Code: Select all

(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

Posted: Tue Aug 19, 2008 9:37 pm
by Lutz
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:

Code: Select all

(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.

Posted: Wed Aug 20, 2008 3:36 pm
by cormullion
Thanks!

What would you call C, in that example? It's not exactly the default function... Default symbol?

Posted: Wed Aug 20, 2008 4:49 pm
by Lutz
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.

Posted: Wed Aug 20, 2008 5:06 pm
by cormullion
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...:)