#!/usr/bin/env auto-editor palet
#lang palet

; Enforce lexical scoping
(define (f x) (lambda (y) (+ x y)))
(assert (equal? ((f 10) 12) 22))

; Test that variables do not leak scope
(define (outer a)
  (define (inner1 b)
    (define (inner2 c) c)
    (inner2 b)
  )
  (inner1 a)
)

(outer 13)
(assert (not (var-exists? 'a)))
(assert (not (var-exists? 'b)))
(assert (not (var-exists? 'c)))

; Test keyword arguments
(define (f1 a b c) (vector a b c))
(define (f2 a #:b #:c) (vector a b c))
;(define (f3 a #:b [#:c 0]) (vector a b c))
;(define (f4 [a 2] [#:b 1] [#:c 0]) (vector a b c))

; Invalid defines
; (define (f [a 2] b c) (vector a b c))
; (define (f a #:b c) (vector a b c))
; (define (f a [#:b 1] #:c) (vector a b c))
; (define (f [a 2] #:b [#:c 0]) (vector a b c))
; (define (f a a #:b) (void))
; (define (f a #:a #:b) (void))

(assert (equal? (f1 3 2 1) #(3 2 1)))
(assert (equal? (f2 3 2 1) #(3 2 1)))
(assert (equal? (f2 3 2 #:c 1) #(3 2 1)))
(assert (equal? (f2 3 #:b 2 #:c 1) #(3 2 1)))
(assert (equal? (f2 3 #:c 1 #:b 2) #(3 2 1)))

; Test `let` and `let*`

(assert (equal? (let ([x 5]) x) 5))
(assert (not (var-exists? 'x)))

(assert (equal?
  (let ([x 5])
    (let ([x 2] [y x]) #(y x))
  )
  #(5 2)
))

(assert (equal?
  (let* ([x 1] [y (+ x 1)]) #(y x))
  #(2 1)
))

(assert (= (apply add1 #(4)) 5))
(assert (= (apply add1 '(4)) 5))
(assert (= (apply sub1 #(4)) 3))
(assert (equal? (map add1 '(3 4 5)) '(4 5 6)))

(assert (equal? ('(3 4 5 6) 0 4 2) '(3 5)))
(assert (equal? (eval '(+ 3 4)) 7))
