I have implemented a stack calculator based on this programming task. I wondered if a more experienced racketeer could give me feedback and tell me if I am missing anything in Racket that would enable the solution to be more elegant.
#lang racket
(define (inc n)
(+ n 1))
(define (print-value s)
(begin
(display s)
(display " ")))
(define (exec l (stack empty) (opcount 1))
"executes a stack based program."
(cond
[(empty? l) (void)]
;; push number onto stack
[(number? (string->number (first l)))
(exec (rest l)
(cons (string->number (first l)) stack)
(inc opcount))]
;; pop number from stack
[(equal? (first l) ".")
(begin
(print-value (first stack))
(exec (rest l) (rest stack)
(inc opcount)))]
;; mathmatical operators
[(equal? (first l) "+")
(exec (rest l) (cons
(+ (first stack) (second stack))
(rest (rest stack))) (inc opcount))]
[(equal? (first l) "-")
(exec (rest l) (cons
(- (first stack) (second stack))
(rest (rest stack))) (inc opcount))]
[(equal? (first l) "*")
(exec (rest l) (cons
(* (first stack) (second stack))
(rest (rest stack))) (inc opcount))]
[(equal? (first l) "/")
(exec (rest l) (cons
(/ (first stack) (second stack))
(rest (rest stack))) (inc opcount))]
;; duplication operator
[(equal? (string-downcase (first l)) "dup")
(exec (rest l)
(cons (first stack) stack) (inc opcount))]
[else
(~a "Error: operation " opcount " invalid")]))
(define program (string-split "64 DUP * ."))
(exec program)