Values in Racket
Racket has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.
#lang racket
; Strings, which can be concatenated with `string-append`
(displayln (string-append "racket" "lang"))
; Integers and floats
(displayln (string-append "1+1 = " (number->string (+ 1 1))))
(displayln (string-append "7.0/3.0 = " (number->string (/ 7.0 3.0))))
; Booleans, with boolean operators as you'd expect
(displayln (and #t #f))
(displayln (or #t #f))
(displayln (not #t))
To run the program, save it as values.rkt
and use the racket
command:
$ racket values.rkt
racketlang
1+1 = 2
7.0/3.0 = 2.3333333333333335
#f
#t
#f
Let’s break down the Racket code:
We start with
#lang racket
to specify that we’re using the Racket language.For string concatenation, we use
string-append
instead of the+
operator.Racket uses prefix notation for arithmetic operations. So
1+1
becomes(+ 1 1)
.To convert numbers to strings for display, we use
number->string
.Booleans in Racket are represented as
#t
for true and#f
for false.The boolean operators
and
,or
, andnot
work similarly to other languages.We use
displayln
to print values with a newline, similar toprintln
in other languages.
Racket’s interactive nature allows for easy experimentation with these basic values and operations in the REPL (Read-Eval-Print Loop), which you can start by simply running racket
without any file argument.