STklos icon indicating copy to clipboard operation
STklos copied to clipboard

Add convenience macros and procedures...

Open jpellegrini opened this issue 10 months ago • 1 comments

More macros and procedures for bonus.stk:

  • dolist (the extremely convenient Common Lisp macro)
  • push!, pop! (Gauche has them, and Common Lisp has PUSH)
  • 1+, 1- (as in Common Lisp, several Schemes have these)
  • inc!, dec! (from Chez and Gauche)

jpellegrini avatar Aug 21 '23 03:08 jpellegrini

Ok, removed inc! and dec!. Thanks @lassik for spotting this.

jpellegrini avatar Aug 21 '23 11:08 jpellegrini

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

egallesio avatar May 02 '24 15:05 egallesio

NB: In Common Lisp the argument order is (push element list)

lassik avatar May 02 '24 16:05 lassik

I don't understand the push! example. Is #((10 1 2 3)) really the correct result?

lassik avatar May 02 '24 16:05 lassik

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> 

egallesio avatar May 02 '24 19:05 egallesio

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.

lassik avatar May 02 '24 20:05 lassik