Racket is an open-source and free software programming language and platform for language design and implementation. Based on over 15 years of research and practical experience, Racket ships with many libraries, an IDE, and a package distribution system.
#lang racket ;; Finds Racket sources in all subdirs (for ([path (in-directory)]) (when (regexp-match? #rx"[.]rkt$" path) (printf "source file: ~a\n" path)))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang web-server/insta ;; A "hello world" web server (define (start request) (response/xexpr '(html (body "Hello World"))))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang racket ; An echo server (define listener (tcp-listen 12345)) (let echo-server () (define-values (in out) (tcp-accept listener)) (thread (lambda () (copy-port in out) (close-output-port out))) (echo-server))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang racket ;; Report each unique line from stdin (let ([saw (make-hash)]) (for ([line (in-lines)]) (unless (hash-ref saw line #f) (displayln line)) (hash-set! saw line #t)))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang racket ; A picture (require 2htdp/image) (let sierpinski ([n 8]) (if (zero? n) (triangle 2 'solid 'red) (let ([t (sierpinski (- n 1))]) (freeze (above t (beside t t))))))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button.
#lang racket/gui ; A GUI guessing game (define f (new frame% [label "Guess"])) (define n (random 5)) (send f show #t) (define ((check i) btn evt) (message-box "." (if (= i n) "Yes" "No"))) (for ([i (in-range 5)]) (make-object button% (format "~a" i) f (check i)))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button.
#lang racket ; Simple web scraper (require net/url net/uri-codec) (define (let-me-google-that-for-you str) (let* ([g "http://www.google.com/search?q="] [u (string-append g (uri-encode str))] [rx #rx"(?<=<h3 class=\"r\">).*?(?=</h3>)"]) (regexp-match* rx (get-pure-port (string->url u)))))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang racket ;; A dice-rolling command-line utility (command-line #:args (dice sides) (for ([i (in-range (string->number dice))]) (displayln (+ 1 (random (string->number sides))))))
This example is a command-line script. To run the example, install Racket, paste the example program into a file, and run racket on the file with command-line arguments after the filename. Alternatively, for a Unix installation, you can add #!/usr/bin/env racket at the top and make the file executable, and then you can run the file directly.
#lang racket ;; Print the Greek alphabet (for ([i (in-range 25)]) (displayln (integer->char (+ i (char->integer #\u3B1)))))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang htdp/bsl ; Any key inflates the balloon (require 2htdp/image) (require 2htdp/universe) (define (balloon b) (circle b "solid" "red")) (define (blow-up b k) (+ b 5)) (define (deflate b) (max (- b 1) 1)) (big-bang 50 (on-key blow-up) (on-tick deflate) (to-draw balloon 200 200))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button.
#lang lazy ;; An infinite list: (define fibs (list* 1 1 (map + fibs (cdr fibs)))) ;; Print the 1000th Fibonacci number: (print (list-ref fibs 1000))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang typed/racket ;; Using higher-order occurrence typing (define-type SrN (U String Number)) (: tog ((Listof SrN) -> String)) (define (tog l) (apply string-append (filter string? l))) (tog (list 5 "hello " 1/2 "world" (sqrt -1)))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang scribble/base @; Generate a PDF or HTML document @title{Bottles --- @italic{Abridged}} @(apply itemlist (for/list ([n (in-range 100 0 -1)]) @item{@(format "~a" n) bottles.}))
To run the example, install Racket, start DrRacket, and paste the example program into the top area in DrRacket. When a program in a Scribble language is opened in DrRacket, a Scribble HTML button appears for rendering the document to HTML. Click it.
#lang racket ; draw a graph of cos (require plot) ; and deriv^3(cos) (define ((deriv f) x) (/ (- (f x) (f (- x 0.001))) 0.001)) (define (thrice f) (lambda (x) (f (f (f x))))) (plot (list (function ((thrice deriv) sin) -5 5) (function cos -5 5 #:color 'blue)))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button.
#lang racket ; Sending email from racket (require net/sendmail) (sleep (* (- (* 60 4) 15) 60)) ; 4h - 15m (send-mail-message (getenv "EMAIL") "Parking meter alert!" (list (getenv "EMAIL")) null null '("Time to go out and move your car."))
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang scheme/base ; Simple use of the FFI (require ffi/unsafe) (define mci-send-string (get-ffi-obj "mciSendStringA" "Winmm" (_fun _string [_pointer = #f] [_int = 0] [_pointer = #f] -> [ret : _int]))) (mci-send-string "play sound.wav wait")
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
#lang datalog ancestor(A, B) :- parent(A, B). ancestor(A, B) :- parent(A, C), D = C, ancestor(D, B). parent(john, douglas). parent(bob, john). ancestor(A, B)?
To run the example, install Racket, start DrRacket, paste the example program into the top area in DrRacket, and click the Run button. Alternatively, save the program to a file and run racket on the file.
Draw more pictures or build a web server from scratch. Racket includes both batteries and a programming environment, so get started!
Racket's interactive mode encourages experimentation, and quick scripts easily compose into larger systems. Small scripts and large systems both benefit from native-code JIT compilation. When a system gets too big to keep in your head, you can add static types.
Extend Racket whenever you need to. Mold it to better suit your tasks without sacrificing interoperability with existing libraries and without having to modify the tool chain. When less is more, you can remove parts of a language or start over and build a new one.
Whether you're just starting out, want to know more about programming language applications or models, looking to expand your horizons, or ready to dive into research, Racket can help you become a better programmer and system builder.