is this passing by reference?

Started by cormullion, August 19, 2008, 01:51:32 PM

Previous topic - Next topic

cormullion

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

Lutz

#1
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.

cormullion

#2
Thanks!



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

Lutz

#3
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.

cormullion

#4
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...:)