You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
beautiful-racket/beautiful-racket-demo/basic-demo-2/expr.rkt

30 lines
709 B
Racket

#lang br
8 years ago
(provide b-expr b-sum b-product b-neg b-expt)
(define (b-expr expr)
(if (integer? expr) (inexact->exact expr) expr))
8 years ago
(define-macro-cases b-sum
8 years ago
[(_ VAL) #'VAL]
[(_ LEFT "+" RIGHT) #'(+ LEFT RIGHT)]
[(_ LEFT "-" RIGHT) #'(- LEFT RIGHT)])
8 years ago
(define-macro-cases b-product
[(_ VAL) #'VAL]
[(_ LEFT "*" RIGHT) #'(* LEFT RIGHT)]
[(_ LEFT "/" RIGHT) #'(/ LEFT RIGHT 1.0)]
8 years ago
[(_ LEFT "mod" RIGHT) #'(modulo LEFT RIGHT)])
8 years ago
(define-macro-cases b-neg
[(_ VAL) #'VAL]
[(_ "-" VAL) #'(- VAL)])
8 years ago
(define-macro-cases b-expt
[(_ VAL) #'VAL]
[(_ LEFT "^" RIGHT) #'(expt LEFT RIGHT)])
8 years ago
(define (b-not expr) (if (zero? expr) 1 0))
(define-macro (b-def ID VAR EXPR)
#'(set! ID (λ (VAR) EXPR)))