A programmable programming language

Racket is a full-spectrum programming language. It goes beyond Lisp and Scheme with dialects that support objects, types, laziness, and more. Racket enables programmers to link components written in different dialects, and it empowers programmers to create new, project-specific dialects. Racket's libraries support applications from web servers and databases to GUIs and charts.

Start Quickly

explain

explain



#lang racket
;; Finds Racket sources in all subdirs
(for ([path (in-directory)]
      #:when (regexp-match? #rx"[.]rkt$" path))
  (printf "source file: ~a\n" path))
 
 


#lang web-server/insta
;; A "hello world" web server
(define (start request)
  (response/xexpr
   '(html
     (body "Hello World"))))
 


#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))


#lang racket
;; Report each unique line from stdin
(define seen (make-hash))
(for ([line (in-lines)])
  (unless (hash-ref seen line #f)
    (displayln line))
  (hash-set! seen line #t))


#lang racket  ; A picture
(require 2htdp/image)
(let sierpinski ([n 8])
  (cond
    [(zero? n) (triangle 2 'solid 'red)]
    [else (define t (sierpinski (- n 1)))
          (freeze (above t (beside t t)))]))


#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)))


#lang racket ; Simple web scraper
(require net/url net/uri-codec)
(define (let-me-google-that-for-you str)
  (define g "http://www.google.com/search?q=")
  (define u (string-append g (uri-encode str)))
  (define rx #rx"(?<=<h3 class=\"r\">).*?(?=</h3>)")
  (regexp-match* rx (get-pure-port (string->url u))))


#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))))))


#lang racket
;; Print the Greek alphabet
(for ([i (in-range 25)])
  (display
   (integer->char
    (+ i (char->integer #\α)))))
 


#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))


#lang lazy
;; An infinite list:
(define fibs
  (list* 1 1 (map + fibs (cdr fibs))))
 
;; Print the 1000th Fibonacci number:
(print (list-ref fibs 1000))


#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)))


#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.}))
 


#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)))


#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."))


#lang racket ; 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")


#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)?

News

Racket version 6.0 is out!

RacketCon 2014 will take place in St. Louis on September 20.

Go Further

Grow your Program

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.

Grow your Language

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.

Grow your Skills

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.

Documentation

For getting started

Quick: An Introduction to Racket with Pictures gives you a taste of Racket.

More: Systems Programming with Racket dives much deeper and much faster, showing how to build a complete continuation-based web server.

Guide: Racket starts with a tutorial on Racket basics, and then it describes the rest of the Racket language.

For experienced Racketeers

Reference: Racket provides comprehensive coverage of all of Racket.

Continue: Web Applications in Racket describes how to use the Racket web server to build dynamic web applications.

Package Management: Racket explains how to install packages, and how to build and distribute your own.

Community

News & Events

RacketCon — The annual Racket meeting, coming up in September. Previously 2013, 2012, and 2011.

Blog — Announcements, helpful hints, and thoughtful rants.

Twitter — Short bits of Racket news.

Discussion

Mailing lists — Discussion lists for using and developing Racket.

IRC — Chat in the #racket channel on freenode.net, an informal discussion channel for all things related to Racket, or browse the logs.

People — The people behind Racket.

Research — Publications and educational resources.

Contributing

Code — The Racket source code on GitHub.

Wiki — Useful pages include Intro Projects and Videos, including tutorials, interviews, and more.

Snapshot builds — The freshest versions of Racket.

Bug reports — File, query, and maybe fix existing reports.

Learning

How to Design Programs

A principled approach to program design.

  • Teaching language support is included with DrRacket.
  • Aimed at the programming novice.
Realm of Racket

Learn Racket and programming, one game at a time.

  • Sample game code comes with the Racket distribution.
  • For those just starting out with Racket.
PLAI

Foundations of programming languages.

  • Understand the features that make languages tick.
  • For undergraduates, graduate students, and experts.
Semantics Engineering with PLT Redex

Lightweight automation for semantics.

  • Model your own programming language semantics.
  • For the working language engineer.
Thanks to the NSF, DARPA, the Fund for the Improvement of Postsecondary Education (FIPSE) at the US Department of Education, the Exxon Foundation, CORD, partners of the Academy of Information Technology, Microsoft, Mozilla, and Google for their generous support over the years.