memory fault with nested assignment

Started by ggeorgalis, February 13, 2011, 07:06:26 PM

Previous topic - Next topic

ggeorgalis

What's wrong here? Isn't evaluation done inside out???



newLISP v.10.3.0 on BSD IPv4/6 UTF-8, execute 'newlisp -h' for more info.

> (numbers (set 'numbers (sequence 1 100)) 5)

ERR: invalid function : (numbers (set 'numbers (sequence 1 100)) 5)
>  (set 'numbers (sequence 1 100))
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100)
> (numbers 5)
6
> (numbers (set 'numbers (sequence 1 100)) 5)
Memory fault

returns to shell.



-George

Lutz

#1
The first case is behaving correctly and according to evaluation rules, which work similar in Scheme and newLISP:



The operator/functor part of a list expression gets evaluated first then the arguments get evaluated. As 'numbers' in the first operator position contains 'nil' the error message "ERR: invalid function" is thrown.



After evaluation 'numbers' should contain either a built-in function, lambda expression, list to be indexed or a number(s) to be used for resting/slicing a list to follow.



The second case should not end with a memory fault, but a list should be indexed with an index vector. As the first operator position contains a list, the second list will be taken as an index vector to index the first list,  which could be nested like in this case:


> (set 'numbers '(1 2 ( 3 4 5)))
(1 2 (3 4 5))
> (numbers '(2 1))
4
>


if you change the nested 'set' statement, it will behave correctly:


> (set 'numbers (sequence 1 10))
(1 2 3 4 5 6 7 8 9 10)
> (numbers (sequence 1 100))
2

the first list will be indexed by '1', the rest of the index vector is thrown away, because the first list is not nested.



Introducing the 'set' statement in the second expression changes the list to be indexed and it fails with a memory page fault, which should not happen and will be fixed.



ps: the last '5' in your example is ignored, is not consumed when indexing a list with an index vector.

ggeorgalis

#2
Has taken me a couple passes to make sense of this.... main thing I didn't realize: "The operator/functor part of a list expression gets evaluated first" but the other examples, I'll be returning to when my brain becomes flexible again. :-) Thanks, -George