is this passing by reference?

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

is this passing by reference?

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks!

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post 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...:)

Locked