Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment and Decrement operators in scheme programming language

What are the increment and decrement operators in scheme programming language. I am using "Dr.Racket" and it is not accepting -1+ and 1+ as operators. And, I have also tried incf and decf, but no use.

like image 478
unknownerror Avatar asked Jun 15 '14 10:06

unknownerror


2 Answers

They are not defined as such since Scheme and Racket try to avoid mutation; but you can easily define them yourself:

(define-syntax incf
  (syntax-rules ()
    ((_ x)   (begin (set! x (+ x 1)) x))
    ((_ x n) (begin (set! x (+ x n)) x))))

(define-syntax decf
  (syntax-rules ()
    ((_ x)   (incf x -1))
    ((_ x n) (incf x (- n)))))

then

> (define v 0)
> (incf v)
1
> v
1
> (decf v 2)
-1
> v
-1

Note that these are syntactic extensions (a.k.a. macros) rather than plain procedures because Scheme does not pass parameters by reference.

like image 82
uselpa Avatar answered Nov 07 '22 06:11

uselpa


Your reference to “DrRacket” somewhat suggests you’re in Racket. According to this, you may already be effectively using #lang racket. Either way, you’re probably looking for add1 and sub1.

-> (add1 3)
4
-> (sub1 3)
2
like image 31
Micah Elliott Avatar answered Nov 07 '22 06:11

Micah Elliott