ref wants a list (array vs. list question)

Started by conan, November 09, 2011, 06:18:27 AM

Previous topic - Next topic

conan

I found that ref wants a list to search for when trying to do this:


(set 'randomMatrix (array 3 5 '(nil)))
(dotimes (x 3)
    (dotimes (y 5)
        (setf (randomMatrix x y) (rand 10))))

(ref '0 randomMatrix)


gives message:

ERR: list expected in function ref : ((8 3 7 7 9) (1 3 7 2 5) (4 6 3 5 9) (9 6 7 1 6) (0 2 1 8 1))



So I changed last line to:


(ref '0 (array-list randomMatrix))

What I was wondering is why there's no way to tell an array from a list visually from the REPL.

I get this:



> randomMatrix

((8 3 7 7 9) (1 3 7 2 5) (4 6 3 5 9) (9 6 7 1 6) (0 2 1 8 1))

> (array-list randomMatrix)

((8 3 7 7 9) (1 3 7 2 5) (4 6 3 5 9) (9 6 7 1 6) (0 2 1 8 1))



It took me some time to think they might not be the same thing internally and to discover it by asking in the REPL:



> (array? randomMatrix)
true
> (list? randomMatrix)
nil




On a side note I was expecting random numbers, but every run spits the same stuff. I realize there must be some initialization going on somewhere (didn't look for it yet). But I think most people expects random stuff to be random and when the need to make a repeatable pseudo-random stuff arise, only then use some pre-seeded function.

cormullion

#1
Use (seed) to prime the random number generator. You might want predictable random numbers for testing purposes...  :)

Lutz

#2
Yes, 'seed' will initialize the random geneartor, but actually it should have printed different random numbers the second time when this is during the same newLISP session - not after exiting newLISP and starting again. What version of newLISP, and is it 64-bit or 32-bit and on what OS, are you using?



About arrays in newLISP: I would always start out using lists and only move to arrays when you have experienced performance problems. Then, if you change to using an array, most of your code can stay the same without any changes. In practice, I have found few programs where arrays make much of a difference.

conan

#3
Quote from: "Lutz"Yes, 'seed' will initialize the random geneartor, but actually it should have printed different random numbers the second time when this is during the same newLISP session - not after exiting newLISP and starting again. What version of newLISP, and is it 64-bit or 32-bit and on what OS, are you using?


Don't worry.



It didn't happen issuing new calls inside the same REPL session, I was reloading the script each time. I talked based on my expectations, I didn't read before what was the proper behavior. Coming from other scripting world I expected different numbers on each run of the script without the need of seeding.


Quote from: "Lutz"About arrays in newLISP: I would always start out using lists and only move to arrays when you have experienced performance problems. Then, if you change to using an array, most of your code can stay the same without any changes. In practice, I have found few programs where arrays make much of a difference.


Good to know then!



I'm just learning the language, and while I was at it I decided it would be a nice pet project for learning to make a sea battle clon. I started using associative lists, using the coordinates as keys:



(("00" nil) ("01" 2) ...)


But then things got complicated and I found arrays, so I switched.



Now I have to rethink it and switch back to list, but using them as arrays.

Lutz

#4
Instead of associative lists you could also try dictionaries/hashes:



http://www.newlisp.org/downloads/newlisp_manual.html#hash">http://www.newlisp.org/downloads/newlis ... .html#hash">http://www.newlisp.org/downloads/newlisp_manual.html#hash



they are fast and scale well.