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
#fLet’s break down the Racket code:
We start with
#lang racketto specify that we’re using the Racket language.For string concatenation, we use
string-appendinstead of the+operator.Racket uses prefix notation for arithmetic operations. So
1+1becomes(+ 1 1).To convert numbers to strings for display, we use
number->string.Booleans in Racket are represented as
#tfor true and#ffor false.The boolean operators
and,or, andnotwork similarly to other languages.We use
displaylnto print values with a newline, similar toprintlnin 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.