egallesio / STklos

STklos Scheme
http://stklos.net
GNU General Public License v2.0
68 stars 17 forks source link

Add convenience macros and procedures... #593

Closed jpellegrini closed 2 months ago

jpellegrini commented 11 months ago

More macros and procedures for bonus.stk:

jpellegrini commented 11 months ago

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

egallesio commented 2 months ago

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
lassik commented 2 months ago

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

lassik commented 2 months ago

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

egallesio commented 2 months ago

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> 
lassik commented 2 months ago

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.