Racket
Online Racket
main.rkt runs in Racket 9.3. Start it with a #lang line: #lang racket/base loads fast and keeps the namespace small, and #lang racket gives you everything. Output isn’t buffered, so a prompt shows before read-line waits.
Try it aboveHow Racket runs here
- Versions
- Racket 9.3
- Run
racket main.rkt- Limits per run
- 256 MB memory, 15 s of CPU time on half a core, 2 MB of output
- Time
- Up to 10 minutes a run (30 on a paid plan); waits 2 minutes for input (5 on a paid plan)
- Sandbox
- Each run gets its own Linux sandbox with no network access. Text files your program writes next to your code open as new tabs.
A Racket program that reads input
This main.rkt reads a line of numbers and uses racket/string and racket/list on them:
#lang racket/base (require racket/string racket/list) (display "Numbers separated by spaces: ") (define nums (map string->number (string-split (read-line)))) (printf "Sum: ~a\n" (apply + nums)) (printf "Sorted: ~a\n" (sort nums <)) (printf "Largest: ~a\n" (argmax values nums))
$ racket main.rkt Numbers separated by spaces: 12 7 30 5 8 Sum: 62 Sorted: (5 7 8 12 30) Largest: 30
Reading input in Racket
- (read-line) returns the line as a string, without the newline.
- (string->number s) converts it, and returns #f rather than an error for text that isn’t a number.
- (read) reads one S-expression, so typing (1 2 3) gives a list.
Errors you’ll see
- “string-join: unbound identifier” The function is in a library you haven’t required, here racket/string, or it’s misspelt.
- “+: contract violation expected: number? given: "2"” A function got the wrong kind of value, often a string that wasn’t converted.
Questions
Which Racket version is this?
Racket 9.3, running main.rkt.
Can I run Scheme code?
Much of it, with #lang racket/base at the top: define, lambda, let and lists work as in Scheme. Racket isn’t strict R7RS, so some details differ.
Can I draw pictures or open windows?
No. Programs write to the terminal; there’s no window to draw in.