#lang racket "--> booleans" (= 2 (+ 1 1)) (boolean? #t) (boolean? #f) (boolean? 0) (boolean? 1.0) (boolean? "no") "any value other than #f counts as true for if, cond, and, or, etc." (if "no" "True for any string" "False") (if "no" "" "False") (if 0 "True for any number" "False") (if -1.0 "True for any number" "False") (if 4/3 "True for any number" "False") (if +nan.0 "True for any number" "False") (if -inf.0 "True for any number" "False") (if 1+2i "True for any number" "False") "--> numbers" " --> exact numbers" " --> integers" 5 99999999999999999 17 " --> rationals" 1/2 99999999999999999/2 -3/4 " --> complexes" 1+2i 1/2+3/4i " --> inexact numbers" " --> IEEE representations" " --> floating-point of a number" 2.0 3.14e+87 " --> infinities" +inf.0 -inf.0 " --> not-a-number" +nan.0 -nan.0 " --> complexes" 2.0+3.0i +inf.0+3.0i -inf.0+nan.0i 2.0-nan.0i 3-nan.0i " --> conversion" ; Computations that involve an inexact number produce inexact results, ; so that inexactness acts as a kind of taint on numbers. ; Beware, however, that Racket offers no “inexact booleans,” ; so computations that branch on the comparison of inexact numbers ; can nevertheless produce exact results. ; (exact->inexact #t) ; error ->inexact: contract violation expected: number? given: #t ; (inexact->exact #t) ; exact: contract violation expected: number? given: #t (/ 1 2) (exact->inexact (/ 1 2)) (exact->inexact 3.0) (exact->inexact +nan.0) ; exact->inexact accepts inexact numbers as input (/ 1 2.0) (inexact->exact (/ 1 2.0)) (inexact->exact 3) ; inexact->exact accepts exact numbers as input ; (inexact->exact +nan.0) ; -> Error : exact: no exact representation for +nan.0 "(inexact->exact 0.1)" (inexact->exact 0.1) "(exact->inexact 3602879701896397/36028797018963968)" (exact->inexact 3602879701896397/36028797018963968) " --> parsing prefixes" #e0.5 ; prefix #e force parsing as an exact number #i3 ; prefix #i force its parsing as an exact #b101 ; prefix #i force its parsing as an exact (2^2 + 2^0 -> 5) #o32 ; prefix #i force its parsing as an exact (3*8 + 2 -> 26) #xF5 ; prefix #i force its parsing as an exact (15*16 + 5 -> 245) " --> rationals" ; Inexact results are also produced by procedures such as sqrt, log, and sin when ; an exact result would require representing real numbers that are not rational. ; Racket can represent only rational numbers and complex numbers with rational parts. (sin 0) ; rational... (sin 1/2) ; not rational... " --> faster with small integers, IEEE faster than large exact numbers" ; In terms of performance, computations with small integers are typically the fastest, ; where “small” means that the number fits into one bit less than the machine’s ; word-sized representation for signed numbers. Computation with very large exact integers ; or with non-integer exact numbers can be much more expensive than ; computation with inexact numbers. (define (sigma f a b) (if (= a b) 0 (+ (f a) (sigma f (+ a 1) b)))) " --> exact numbers" (time (round (sigma (lambda (x) (/ 1 x)) 1 2000))) " --> inexact numbers" (time (round (sigma (lambda (x) (/ 1.0 x)) 1 2000))) " --> test number categroy" (integer? 5) (integer? 5.0) (integer? 5.1) (integer? +inf.0) (integer? 1+2i) (complex? 5) (complex? 1+2i) (complex? 1.0+2.0i) (real? 1) (real? 1+2i) (real? +inf.0) (number? 1) (number? 1+2i) (number? +inf.0) (rational? (sin 1/2)) ; ??? (abs -5) ; (abs -5+2i) ; error : contract violation expected: real? given: -5+2i (sin -5+2i) " --> eqv?)" (= 1 1.0) (eqv? 1 1.0) (= 1/2 0.5) (eqv? 1/2 0.5) "Beware of comparisons involving inexact numbers," "which by their nature can have surprising behavior." "Even apparently simple inexact numbers may not mean what you think they mean;" "for example, while a base-2 IEEE floating-point number can represent 1/2 exactly," "it can only approximate 1/10" (= 1/2 0.5) (= 1/10 0.1) (inexact->exact 0.1) "--> apply" (define (avg lst) (/ (apply + lst) (length lst))) (apply - '(1 2 3)) ; 1 - 2 - 3 ==> -4 (apply - 0 '(1 2 3)) ; 0 - 1 - 2 - 3 ==> -6 "--> Characters" " --> Literals" #\A #\λ #\u03BB #\space " --> Diplay and write" (display #\A) (display #\newline) (write #\A) (display #\newline) " --> Conversions" (integer->char 65) (char->integer #\A) (integer->char 17) (char->integer #\space) " --> Checks" (char-alphabetic? #\A) (char-numeric? #\0) (char-whitespace? #\newline) (char-downcase #\A) (char-upcase #\ß) (char=? #\a #\A) (char-ci=? #\a #\A) (eqv? #\a #\A) "--> Characters" " --> Literals" "Apple" "\u03BB" "" " --> Diplay and write" (display "Apple") (display #\newline) (write "Apple") (display #\newline) (display "a \"quoted\" thing") (display #\newline) (write "a \"quoted\" thing") (display #\newline) (display "two\nlines") (display #\newline) (write "two\nlines") (display #\newline) (display "\u03BB") (display #\newline) (write "\u03BB") (display #\newline) " --> Acces car" (string-ref "Apple" 0) (string-ref "Apple" 3) " --> Create and modify mutbales strings" (define s (make-string 5 #\.)) (string-set! s 2 #\λ) (string? s) s (define s2 (string #\A #\p #\p #\l #\e)) (string-set! s2 4 #\y) s2 (string? s2) (define s3 "Apple") (string? s3) ; (string-set! s3 2 #\λ) ; contract violation expected: mutable-string? given: "Apple" " --> sort" (sort '("Coucou" "apple" "Banana") string<?) (string<? "apple" "Banana") (sort '("Coucou" "apple" "Banana") string-ci<?) (string-ci<? "apple" "Banana") (string-upcase "Straße") " --> other ???" (string-upcase "Straße") (parameterize ([current-locale "C"]) (string-locale-upcase "Straße")) (define p (cons 1 4)) (pair? p) (define p2 '( 1 4)) (pair? p2) (eqv? (cons 1 4) '( 1 4)) "--> Bytes and Byte Strings" (byte? 0) (byte? 255) (byte? 256) (byte? -1) (byte? #\a) (define bs #"Apple") (display bs) (display #\newline) (write bs) (display #\newline) (bytes-ref #"Apple" 0) (make-bytes 3 65) (define b (make-bytes 2 0)) b (bytes-set! b 0 1) (bytes-set! b 1 255) b ; Affichage en otcal #o377 (display #"Apple") (display #\newline) (display "\316\273") ; same as "λ" (display #\newline) (display #"\316\273") ; UTF-8 encoding of λ (display #\newline) (bytes->string/utf-8 #"\316\273") (bytes->string/latin-1 #"\316\273") ; (parameterize ([current-locale "C"]) ; C locale supports ASCII, only, ; (bytes->string/locale #"\316\273")) ; so... ; Error : byte string is not a valid encoding for the current locale byte string: #"\316\273" (let ([cvt (bytes-open-converter "cp1253" ; Greek code page "UTF-8")] [dest (make-bytes 2)]) (bytes-convert cvt #"\353" 0 1 dest) (bytes-close-converter cvt) (bytes->string/utf-8 dest)) "--> Symbols" 'a (symbol? 'a) (eq? 'a 'a) (eq? 'a (string->symbol "a")) (eq? 'a 'b) (eq? 'a 'A) #ci'A (eq? #ci'A #ci'a) (eq? #ci'A #\a) (eq? #ci'A 'A) (eq? #ci'A 'a) (eqv? #ci'A #ci'a) (eqv? #ci'A 'a) (eqv? #ci'A 'A) (define (shower value) (display value) (display #\newline) (write value) (display #\newline) value ) (define sym1 (string->symbol "one, two")) (shower sym1) (define sym2 '|mon "Symbol" long|) (shower sym2) (define sym3 (string->symbol "6")) (shower sym3) (define sym4 'Hello) (shower sym4) (define sym5 (gensym)) (shower sym5) (define sym6 (gensym)) (shower sym6) "--> keywords" "--> Pairs and lists" ; #<void> is return value for display function, so '(#<void> #<void> #<void> #<void> #<void> #<void> #<void>) ; is result of map display over content (map and for both produce list of results, so for returns the same result). ; If you want to run some side effect for each element in the list, you should rather use for-each. ; Also, don't use define inside define, use let for introducing local variables, and consider using displayln. (define (showlist l) (displayln "debut") (for-each displayln l) (displayln "fin")) (showlist '(1 2 3 4 5)) (showlist '((1 2) 3 (4 5))) (showlist '(1 ((2 3) (4 5))))
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 ...+)