#lang racket
(for ([path (in-directory)])
(when (regexp-match? #rx"[.]rkt$" path)
(printf "source file: ~a\n" path)))
The in-directory function constructs a sequence that
walks a directory tree (starting with the current directory, by default)
and generates paths in the tree. The for form binds
p to each path in the sequence, and regexp-match?
applies a pattern to the 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
(define (start request)
(response/xexpr
'(html
(body "Hello World"))))
This example implements a web server using the
web-server/insta language. Each time a connection is made to
the server, the start function is called to get the HTML to
send back to the client.
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
(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))
Racket makes it easy to use TCP sockets and spawn threads to handle
them. This program starts a server at TCP port 12345 that echos
anything a client sends back to the client.
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
(let ([saw (make-hash)])
(for ([line (in-lines)])
(unless (hash-ref saw line #f)
(displayln line))
(hash-set! saw line #t)))
Uses a hash table to record previously seen lines. You can run this
program in DrRacket, but it makes more sense from the command line.
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
(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))))))
The 2htdp/image library provides easy-to-use functions
for constructing images, and DrRacket can display an image result as
easily as it can display a number result. In this case, a
sierpinski function is defined and called (at the same time)
to generate a Sierpinski triangle of depth 8.
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
(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)))
This simple guesing game demonstates Racket's class-based GUI
toolkit. The frame% class implements a top-level window, and
button% obviously implements a button. The check
function defined here produces an function that is used for the button's
callback action.
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
(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)))))
Add a call to let-me-google-that-for-you to get a list of
search results.
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
(command-line
#:args (dice sides)
(for ([i (in-range (string->number dice))])
(displayln
(+ 1 (random (string->number sides))))))
Playing a game but no dice on hand? Let Racket roll for you. The
command-line form makes sure that the right number of
arguments are provided and automatically implements the --help
switch.
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
(for ([i (in-range 25)])
(displayln
(integer->char
(+ i (char->integer #\u3B1)))))
The only reason we use the encoded form of a character
#\u3B1 instead of the more direct form #\α is that
we don't trust your browser to render it correctly. DrRacket is
perfectly happy with #\α.
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
(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))
Racket's mission includes education at all levels. This program
uses the htdp/bsl teaching language, the
2htdp/image library for creating pictures in the teaching
languages, and the 2htdp/universe library for interactive
animations.
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
(define fibs
(list* 1 1 (map + fibs (cdr fibs))))
(print (list-ref fibs 1000))
And now for something completely different. The lazy
language is more like Haskell than Lisp, so feel free to build an
infinite list and look at only part of it.
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
(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)))
Racket's type system is designed to let you add types after you've
worked for a while in untyped mode — even if your untyped program
wouldn't fit nicely in a conventional type system.
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
@title{Bottles --- @italic{Abridged}}
@(apply itemlist
(for/list ([n (in-range 100 0 -1)])
@item{@(format "~a" n) bottles.}))
This program uses the scribble/base language for
generating documents using a prose-friendly syntax.
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
(require plot)
(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)))
This program uses the plot library to draw plots of
functions. Note that the plots are actual value, which DrRacket shows
in graphical form.
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
(require net/sendmail)
(sleep (* (- (* 60 4) 15) 60))
(send-mail-message
(getenv "EMAIL") "Parking meter alert!"
(list (getenv "EMAIL")) null null
'("Time to go out and move your car."))
Racket comes with plenty of libraries.
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
(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")
Using the FFI means that you're not limited to using Racket
libraries: pulling out a foreign function is easy, and can even be done
dynamically on the REPL.
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)?
Racket is useful for building other languages. This example uses
the pre-packaged Racket implementation of Datalog, a logic programming
language. If you use this from DrRacket, you'll see that it provides
proper highlighting, Check Syntax, and a Datalog-specific REPL.
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.