A tour of Scheme+

The output of tour+.rkt, a Scheme+ program compiled to JavaScript by RacketScript and run by your browser. The same program gives the same output in Racket and in DrRacket. Back



tour+.rkt

#lang reader SRFI-105

;; A tour of Scheme+ : the curly infix expressions and the forms of Scheme+ , in a program that runs the same in
;; Racket and in JavaScript (compiled by RacketScript).

;; Author: Damien Mattei

;; In DrRacket : open this file and click Run (or in a terminal : racket tour+.rkt).
;;
;; In JavaScript , with RacketScript (raco pkg install racketscript , it needs Node.js) :
;;   racks -d js tour+.rkt
;;   node js/modules/tour+.rkt.js
;; and in a web browser : the page tour.html (see build.sh).
;;
;; The output is the same in Racket , in Node.js and in the browser.

(require Scheme+)

;; the operator + is overloaded at the beginning of the module (before any use of +) : it also concatenates strings
(overload-operator + string-append (string?))


(define (title text)
  (newline)
  (displayln text))

(define (show label value)
  (display "  ")
  (display label)
  (display " : ")
  (displayln value))


(displayln "A tour of Scheme+")


(title "1. Infix expressions , with the precedence of the operators")
(show "{3 * 5 + 2}" {3 * 5 + 2})
(show "{3 + 5 * 2}" {3 + 5 * 2})
(show "{(3 + 1) * (2 * 3 - 1)}" {(3 + 1) * (2 * 3 - 1)})
(show "{3 * (- 9 4) + 2} , a prefix expression inside an infix one" {3 * (- 9 4) + 2})
{x ⥆ 3}
(show "{x ² + 2 * x + 1} with x = 3 , a superscript is an exponent" {x ² + 2 * x + 1})
(define n 5)
(show "{2 ⁿ⁻¹ + 1} with n = 5" {2 ⁿ⁻¹ + 1})
(show "{(x > 2) and (x < 4)}" {(x > 2) and (x < 4)})


(title "2. The infix if")
(show "{\"big\" if (x > 10) else \"small\"}" {"big" if (x > 10) else "small"})
(show "{(x ²) if (x > 0) else 0}" {(x ²) if (x > 0) else 0})


(title "3. Definitions with ⥆ and assignments with ←")
{s ⥆ 0}
{s ← s + 3}
{s ← s * 2}
(show "{s ⥆ 0} {s ← s + 3} {s ← s * 2} , s" s)
{a ⥆ b ⥆ 7}
(show "{a ⥆ b ⥆ 7} , (list a b)" (list a b))


(title "4. Procedures with return and return-rec")

(def (first-negative L)
  (for-each (λ (x) (when {x < 0} (return x))) L)
  #f)
(show "(first-negative '(3 5 -2 7))" (first-negative '(3 5 -2 7)))
(show "(first-negative '(3 5 7))" (first-negative '(3 5 7)))

(def (find-zero L) ;; return-rec returns from all the recursive calls at once
  (cond ((null? L) #f)
        ({(car L) = 0} (return-rec 'found))
        (else (find-zero (cdr L)))))
(show "(find-zero '(4 3 0 2))" (find-zero '(4 3 0 2)))

(define sign (lambda+ (x)
               (when {x > 0} (return 'positive))
               (when {x < 0} (return 'negative))
               'zero))
(show "(map sign '(5 -5 0)) , with lambda+" (map sign '(5 -5 0)))


(title "5. The for of Scheme+ , with break and continue")

{sum ⥆ 0}
(for ({i ⥆ 1} {i ≤ 10} {i ← i + 1})
  {sum ← sum + i})
(show "the sum of the integers from 1 to 10" sum)

{sum ← 0}
(for ({i ⥆ 1} {i ≤ 100} {i ← i + 1})
  (when {i ² > 50} (break))
  {sum ← sum + i})
(show "the sum of the i while i ² ≤ 50 (break)" sum)

{sum ← 0}
(for ({i ⥆ 1} {i ≤ 10} {i ← i + 1})
  (when (even? i) (continue))
  {sum ← sum + i})
(show "the sum of the odd integers from 1 to 10 (continue)" sum)


(title "6. Square brackets on vectors and strings")
{v ⥆ (vector 0 10 20 30 40 50)}
(show "v" v)
(show "{v[2]}" {v[2]})
(show "{v[-1]} , a negative index counts from the end" {v[-1]})
(show "{v[1 : 4]} , a slice" {v[1 : 4]})
(show "{v[: 2]}" {v[: 2]})
(show "{v[3 :]}" {v[3 :]})
{v[0] ← v[5] + 1}
(show "after {v[0] ← v[5] + 1} , v" v)
(show "{\"elephant\"[2 : 5]}" {"elephant"[2 : 5]})
(show "{\"hello\"[-1]}" {"hello"[-1]})


(title "7. Mathematical operators")
(show "{3 ≤ 4 ≤ 5}" {3 ≤ 4 ≤ 5})
(show "{7 ≥ 8}" {7 ≥ 8})
(show "{3 ≠ 4}" {3 ≠ 4})
(show "{2 · 3 + 1}" {2 · 3 + 1})
(show "{(√ 16) + 1}" {(√ 16) + 1})
(show "(map √ '(1 4 9 16))" (map √ '(1 4 9 16)))
(show "{12 ∣ 3} , bitwise or" {12 ∣ 3})
(show "{12 & 10} , bitwise and" {12 & 10})
(show "{1 << 4} , shift" {1 << 4})


(title "8. An overloaded operator")
(show "{\"Scheme\" + \"+\"} , + on strings" {"Scheme" + "+"})
(show "{2 + 3} , + on numbers" {2 + 3})