3 ms·
What's the difference between PUSH and CONS? Also, what does the free-list register do?
by ogogmad 4y ago
What's the difference between PUSH and CONS?
Also, what does the free-list register do?
- ravi-delia 4y agoFor the former, "push x list" is to "cons x list" as "var += 7" is to "var + 7". It's just syntactic sugar to modify a variable.
- twic 4y agoPUSH is just an alias for CONS, useful for emphasising that you're working with a stack. The free list holds all the cons cells not in use. Note that in this machine, there is no way to create or destroy cons cells - that's the whole point! Rather, when you need to cons some values, you pull a cell off the free list and populate it (see the implementation of CONS), and when you want to throw a cons cell away, you put it back on the free list (see the implementations of POP and FREE).
- mtreis86 4y agoThe main difference, in Common Lisp at least, is that push modifies the list to add the new cell. Cons returns a new list with the cell added so the old list remains untouched. CL-USER> (let ((list (list 1 2 3 4))) (cons 5 list) list) (1 2 3 4) CL-USER> (let ((list (list 1 2 3 4))) (push 5 list ) list) (5 1 2 3 4)
- hayley-patton 4y agoCareful - push is a macro which modifies the value bound to "list", but doesn't modify any of the cons cells in that list.
- mtreis86 4y agoSo no rplaca/d, just cons and setf?
- hayley-patton 4y agoRight.