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:

  1. We start with #lang racket to specify that we’re using the Racket language.

  2. For string concatenation, we use string-append instead of the + operator.

  3. Racket uses prefix notation for arithmetic operations. So 1+1 becomes (+ 1 1).

  4. To convert numbers to strings for display, we use number->string.

  5. Booleans in Racket are represented as #t for true and #f for false.

  6. The boolean operators and, or, and not work similarly to other languages.

  7. We use displayln to print values with a newline, similar to println 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.