Passing paramters by value

Started by HJH, June 06, 2005, 02:50:15 AM

Previous topic - Next topic

HJH

Hi



I have a general question of understanding

after having read

http://www.newlisp.org/MemoryManagement.html">//http://www.newlisp.org/MemoryManagement.html



It seems that parameters are always passed by value to the functions. This means that a parameter value is copied, i.e. duplicated, when calling a function. This seems to be the case even for lists.



So if I have a function with a list as an argument which calls itself recursively let's say 50 times, do I have then 50 times a copy of the same list in the memory? Or am I missing something?



Thank you for the answer in advance

--HJH

Lutz

#1
You can pass a list or any other data object by reference using contexts as described in the manual chapter 16 "Programming with context objects" in the sub chapter: "Passing objects by reference":



;; pass an object by reference

(set 'mydb:lst (sequence 1 10000))

(define (change-db obj idx value)
(nth-set idx obj:lst value))

(change-db mydb 1234 "abcdefg")

(nth 1234 mydb:lst)     => "abcdefg"


and of course you could pass the database by symbol without the usage of contexts:



(define (change-db lst idx value)
    (nth-set idx (eval lst) value))

(change-db 'mydb 1234 "abcdefg") ; quoted 'mydb

(nth 1234 mydb) => "abcdefg"


both methods work at same speed, but the first one may be easier to read/understand.



Lutz

HJH

#2
Thank you for the answer. In fact for me the second version with passing the large data structure (the data base list) as a symbol is easier to understand.



--HJH

PaipoJim

#3
Quote from: "Lutz"
...
[code]
(define (change lst idx value)
    (nth-set idx (eval lst) value))

(change-db 'mydb 1234 "abcdefg") ; quoted 'mydb
...

Lutz

Shouldn't that be:  (change 'mydb 1234 "abcdefg")  ?
.

Lutz

#4
Yes, I corrected the original post to all 'change-db'



Lutz

PaipoJim

#5
This inconsistent function name also occurs in the "Design elements and patterns" doc:



Chapter 6. Creating accessing and modifying lists



section: Passing lists by reference


Quote
(define (change-data obj i value)

   (nth-set i obj:data value))



(set 'DB:data '(a b c d e f))



(change DB 3 'x) => d



DB:data => (a b c x e f)

Lutz

#6
Thanks for the correction. A new version is posted here: http://newlisp.org/DesignPatterns.html">http://newlisp.org/DesignPatterns.html



Lutz

HJH

#7
Thank you for this links to the design patterns page. It is very useful. I was not aware of it.



A suggestion for the future:



A section of a side by side comparison of some of the more prominent patterns with COMMON LISP would be useful. I am currently learning COMMON LISP (CLISP and ACL) as well so I am willing to contribute if I am pointed to the right spots where to start working out the differences.



A nice thing would be as well something like a "bilingual dictionary" which gives the translations (or only rough equivalents if there is a difference in meaning) between the predicates and function of newLisp and COMMON LISP. Even an incomplete list will be helpful. I am currently only using 30 functions / predicates of newLisp and can do useful things with it.



My aim is to translate the script I just did to COMMON LISP because I would like to get a feeling to see how different it is and to give it to people who probably are not willing to experiment with newLisp.



I may post this 30 word lists of functions / predicates, if you like.

(it is about file IO, list manipulation and string manipulation. With these functions you can already accomplish a lot in general data processing or more precisely - scripting).



--HJH





BTW Has somebody a newLisp function which takes the name of a newLisp source file as input and produces a statistics of the built-in predicates used?

(the result beeing a list of key-value paris; the key is newLisp predicate/function, value is the number of times the thing is used)

Lutz

#8
Try this one:

;; count primes in a newLISP source file
;;
;; returns a list of built in function symbols in
;; a newLISP source file sorted by frequency
;;
;; example:
;;
;;    (count-primes "somefile.lsp")

(define (count-primes file-name)
    (letn ( (tkns (parse (read-file file-name)))
            (utkns (unique tkns))
            (primes (filter (fn (s) (primitive? (eval (sym s)))) utkns) ))
        (sort (map list (count primes tkns) (map sym primes)) > )
    )
)


Lutz