STklos
STklos copied to clipboard
Add convenience macros and procedures...
More macros and procedures for bonus.stk
:
-
dolist
(the extremely convenient Common Lisp macro) -
push!
,pop!
(Gauche has them, and Common Lisp hasPUSH
) -
1+
,1-
(as in Common Lisp, several Schemes have these) -
inc!
,dec!
(from Chez and Gauche)
Ok, removed inc!
and dec!
. Thanks @lassik for spotting this.
Hi @jpellegrini,
I have finally merged this PR. I have modified your code since, as remarked by @lassik, we have generalized set!
. This has to be taken into account for inc!
and dec!
but also for push!
. For instance,
stklos> (define v (vector 1 2 3))
;; v
stklos> (push! (vector-ref v 0) 10)
stklos> v
#((10 1 2 3))
Since the implementation of push!
is similar to the implementation of inc!
and dec!
, I have reintroduced them in this PR merging.
For dolist
, I have permitted to have a result as we have in dotimes
:
(let ((sum 0))
(dolist (x '(1 2 3 4 5) sum)
(inc! sum (square x)))) => 55
NB: In Common Lisp the argument order is (push element list)
I don't understand the push!
example. Is #((10 1 2 3))
really the correct result?
NB: In Common Lisp the argument order is
(push element list)
Yes, but in Scheme, functions on structured objects takes the object as first arg (list-ref
, vector-ref
, vector-set!
...).
This is also the order used by Gauche.
I don't understand the push! example. Is #((10 1 2 3)) really the correct result?
Nope!
In this case, it is #((10 . 1) 2 3)
. I wanted to avoid the dotted pair and have redefined v
. The example should have been
stklos> (define v (vector (list 1 2 3)))
;; v
stklos> (push! (vector-ref v 0) 10)
stklos> v
#((10 1 2 3))
stklos>
NB: In Common Lisp the argument order is
(push element list)
Yes, but in Scheme, functions on structured objects takes the object as first arg (
list-ref
,vector-ref
,vector-set!
...). This is also the order used by Gauche.
Fair enough.
The example should have been
stklos> (define v (vector (list 1 2 3))) ;; v stklos> (push! (vector-ref v 0) 10) stklos> v #((10 1 2 3)) stklos>
Thanks. That makes sense.