#lang racket ;-----------------; ;utility functions; ;-----------------; (define (token->symbol token) (cond [(equal? token "+") +] [(equal? token "-") -] [(equal? token "*") *] [(equal? token "/") /] [(equal? token "1") 1] [(equal? token "2") 2] [(equal? token "3") 3] [(equal? token "4") 4] [(equal? token "5") 5] [(equal? token "6") 6] [(equal? token "7") 7] [(equal? token "8") 8] [(equal? token "9") 9] [else token])) (define (str->symbol-list str) (map token->symbol (regexp-split #rx" +" (string-trim (regexp-replace* #px"(\\+|-|\\*|/|\\(|\\))" str " \\1 "))))) (define (braces-match? lhs rhs) (cond [(and (equal? lhs "(") (equal? rhs ")")) #true] [(and (equal? lhs "[") (equal? rhs "]")) #true] [(and (equal? lhs "{") (equal? rhs "}")) #true] [else #false])) (define (precedence op) (cond [(member op (list + -)) 1] [(member op (list * /)) 2] [else 0])) (define (snoc item container) (append container (list item))) ;------------------------; ;rpn evaluation functions; ;------------------------; (define (eval-rpn-eval expr calc) (let* ([op (car expr)] [lhs (cadr calc)] [rhs (car calc)]) (op lhs rhs))) (define (eval-rpn expr) (if (< (length expr) 1) 0 (eval-rpn-impl expr '()))) (define (eval-rpn-impl expr calc) (cond [(= (length expr) 1) (eval-rpn-eval expr calc)] [(number? (car expr)) (eval-rpn-impl (cdr expr) (cons (car expr) calc))] [else (eval-rpn-impl (cdr expr) (cons (eval-rpn-eval expr calc) (cddr calc)))])) ;---------------------------------; ;infix to rpn conversion functions; ;---------------------------------; (define (in->postfix expr) (if (null? expr) '() (in->postfix-impl (snoc ")" (str->symbol-list expr)) (cons "(" '()) '()))) (define (in->postfix-impl expr stack queue) (if (null? expr) (append queue stack) (cond [(number? (car expr)) (in->postfix-impl (cdr expr) stack (snoc (car expr) queue))] [(member (car expr) (list + - * /)) (if (< (precedence (car expr)) (precedence (car stack))) (in->postfix-impl expr (cdr stack) (snoc (car stack) queue)) (in->postfix-impl (cdr expr) (cons (car expr) stack) queue))] [(member (car expr) (list "(" "[" "{")) (in->postfix-impl (cdr expr) (cons (car expr) stack) queue)] [(member (car expr) (list ")" "]" "}")) (if (braces-match? (car stack) (car expr)) (in->postfix-impl (cdr expr) (cdr stack) queue) (in->postfix-impl expr (cdr stack) (snoc (car stack) queue)))] [else (in->postfix-impl '() '() (list "invalid character"))]))) ;-----; ;tests; ;-----; (eval-rpn(in->postfix "1/(2-(3))"))
Write, Run & Share Racket code online using OneCompiler's Racket online compiler for free. It's one of the robust, feature-rich online compilers for Racket language, running on the latest version 6.8. Getting started with the OneCompiler's Racket compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Racket
and start coding.
OneCompiler's Racket online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Racket program which takes name as input and print your name with hello.
#lang racket/base
(define name (read))
(printf "Hello ~a.\n" name)
Racket is a general-purpose programming language based on the Scheme dialect of Lisp. It is also used for scripting, computer science education, and research related applications.
Item | Decsription |
---|---|
; | To comment a single line |
;; | to mark important comments |
#; | to comment the following s-expression |
Data-type | Decsription |
---|---|
Numbers | represents integers, float and complex numbers |
Boolean | #t and #f are the two boolean literals |
Strings | To represent sequence of characters and double quotes("") are used to represent strings |
let and define are used to declare variables
(let ([id value-expression] ...) body ...+)
(let proc-id ([id init-expression] ...) body ...+)
define id expression
> (let ([x 10]) x)
10
If, If-else are used when you want to perform a certain set of operations based on conditional expressions.
(if cond-expr then-expr)
(if cond-expr then-expr else-expr)
For loop is used to iterate a set of statements based on a condition.
(for (for-clause ...) body-or-break ... body)
where
for-clause = [id seq-expr] | [(id ...) seq-expr] | #:when guard-expr | #:unless guard-expr | break-clause
break-clause = #:break guard-expr | #:final guard-expr
body-or-break = body | break-clause
seq-expr : sequence?
A lambda expression is used to create a function.
(lambda (argument-id ...)
body ...+)