Racket Summary:
Run either with xwindows support for putty Or just run it on your mac or pc.
Download and install Dr. Racket from http://download.racket-lang.org/
Choose language – the racket language with #lang racket
Help on a command in Dr. Racket : click on a command and press F1 for context sensitive help.
Some helpful links:
Description |
Link |
Dr. Racket Guide |
|
Quick introduction stepping you through. I highly recommend this for those who want a hands on tutorial that is short |
http://docs.racket-lang.org/quick/index.html |
Good summary of Racket syntax |
|
Great discussion of the definition of functional programming |
|
Helpful discussion on stack overflow about why variable mutation is not emphasized: |
|
Why functional programming? |
http://www.infoq.com/presentations/Taming-Effect-Simon-Peyton-Jones |
Functional programming has its roots in lambda calculus, so take a look at Wikipedia lambda calculus description |
|
|
|
Modules:
· For Dr. Bloch: (require picturing-programs)
· For Learn X in Y min: #lang slideshow
· For test cases: (require test-engine/racket-tests)
Comments:
·
Block
comments: #| … |#
·
Single
comments: ;
Literals
·
String:
" " (use \" to type a text
quote)
·
number:
1, 1.3, 1/2, 1+2i, 6.003e+15, #x1A, #b10111, #o737, 8888888888888888888
o
will
store a rational
·
true/false
: #t for true, #f for false
·
logical:
not, and, or : ex: (not #t) is false and (and 1 2) is false
·
Suppress
expansion: just one leading ' : '(function
a b) will be text not a function
·
Do
not put a literal inside ()or Racket will evaluate it as a function
Variables (but
purely functional will not change variable values throughout the process of a
program – it will instead act as an Excel spreadsheet with a final target
value)
·
definition:
(define varname value) example: (define x 3)
·
use:
just use the name; example: x
·
define
locally inside a let expression: (let ([varname value])
expression )
o
use
let * if you want to use the first set of variables to define another set
·
use
a variable: just the name - do not put a variable inside () or Racket will
evaluate it as a function
·
change a variable: (set! varname value)
example: (set!
n
(add1
n))
Screen
reading and writing:
·
read in a variable from the
screen (read var) example : (read n)
o
if it is a number, it will
be stored as a number
·
write a variable to the
screen:
o just put the variable name
o
final
result of all functions write to the screen
o
literal
alone will write to the screen
Arithmetic:
·
All
arithmetic is a function
·
syntax:
( operator operand#1 operand#2)
·
operators:
+,-,/,*,expt,quotient, remainder
o
expt : returns first parm raised to power of
second parm (expt 2 3) returns
8
o
remainder:
returns remainder of first parm divided by second parm (remainder 7 2) returns 1
·
special
operators: exact->inexact (from rational to real), gcd,
lcm, floor, truncate
o
truncate
– converts to decimal and chops after the decimal point. (truncate
-2.5) is -2.
o
floor –
returns the largest integer that is smaller than the number (truncate -2.5) is -3.
Comparison
·
returns
#t or #f
·
is equal?:
= for numeric identity and eq? for object identity
o
numeric
eval =: (=
first second) ex: (= 3 3.0) results in #t
o
object
eval eq?: (eq? first second) ex (eq? 3 3.0)
results in #f
·
is not
equal? : use not function after equal
o
(not (eq? "abc"
"def")) results in #t
·
<,
> , <=, >=,
String functions
·
(string-append
first second) resulting in concatenation
·
(string-ref
first index) pulling out a character from a string
·
(string-replace
string oldchars newchars)
replaces a portion of a string with another string (Note that this function may
need (require racket/string)
·
(string->number string) returns a number representation of
the string unless it cannot – in which case it returns false .
(string->number "30") returns 30
·
(format
" a format string with ~a as any variable type " first~ second~ third~)
formats a string
·
(printf "prints out a string on the screen")
Structs
·
structure
definition: (struct structname
(varname1 varname2 varname3))
o
(struct student (name gpa age))
·
variable
of structure type definition: (define varname (structname value1 value2 value3))
o
(define
s1 (student "amy jones" 3.0 20))
·
use
of entire struct variable: varname
o
s1
(will resolve to #<student>)
·
use
of one part of struct variable: (structname-varname1 varname)
o
(student-name
s1) will resolve to "amy jones"
·
check type
: structname? variable
o
(s1?
student ) will give #t
Pairs
·
Define
a pair: (cons first second) example (cons 3 4), which prints as (1 . 2)
·
Pull
out the first element of a pair (car (cons 3 4))
·
Pull
out the second element of a pair (cdr (cons 3 4))
Lists
·
Define
a list: linked lists of pairs with one side being the list item and the other being
the rest of the list, that ends in null or ()
o
(cons
1 (cons 2 (cons 3 null)))
o
(list
1 2 3)
o
quote
for the literal value
§
'(1
2 3)
o
(build-list
10 values) to get 0 to 10
o
(string-split
string delim) to split a string into a list using
some character marker. ex: (string-split "ab,cd,ef"
",") to get '(ab cd ef)
·
Add
items to list:
o
add
items to a list using cons (cons 4 '(1 2 3 ))
o
add
items to a list using append (append '(1 2) '(3 4))
·
List
functions
o
Do
the same thing to every item in the list (map add1 '(1 2 3)) gives '(2 3 4)
o
Apply
one list to another list of the same number of items (map + '(1 2 3) '(10 20 30))
o
extract
only even (filter even? '(1 2 3 4)) gives '(2 4)
o
count only
even (count even? '(1 2 3 4)) gives 2
o
take
the first n items (take '(1 2 3 4 ) 2) gives '(1 2 )
o
drop
the first n items (drop '(1 2 3 4 ) 2) gives '(3 4)
o
is it
a member? (member 'dog '(cat dog rat))
gives #t
o
first
member value (first '(3 4 5)) gives 3
o
get
number of elements (length '(3 4 5)) gives 3
Functions
·
The
first word inside parentheses is treated as a function
·
definition:
o
Using
define: (define (functionName anyVar
anyVar anyVar) ( definition
of function))
§
(define
(hello-world) "Hello World" "goodbye")
§
(hello-world)
results in "goodbye"
§
(define
(add3 num) (+ num 3))
§
(add3
5) results in 8
o
using
lambda
§
(lambda
() "Hello") is only a
definition
§
((
lambda () "Hello")) results in "Hello"
·
note
use of () to call a function
§
(lambda
(num) (+ num 3))
§
((lambda
(5) (+ num 3)) ) results in 8
o
Using
define and lambda – assign a lambda function to a symbol
§
(define
Lhello-world ( lambda() "Hello"))
§
(Lhello-world) results in "Hello"
§
(define
Ladd3 ( lambda (num) (+ num
3)))
§
(Ladd3
5) results in 8
o
definition does not cause result. When is called, the functions rules will
be used.
o
Functions
return the value of their last expression
o
Can
have mutli-variadic functions (overloaded) but place
all in one definition with case-lambda
·
Pure
function – no side effects (display, input/output, variable change)
o
A
call with certain inputs always returns the same outputs
o
Great
for testing
Function
testing
o
include knowledge of creating test cases:
(require test-engine/racket-tests)
o
code
tests: (check-expect (function call) result)
o
run
tests: (test)
Random number
·
(random
#)
o
#
is number of choices, starting at 0
o
(random
6) gives 0 to 5
o
leave
# blank to get number between 0 and 1
Conditions
·
if/else
: (if test true-expression
false-expression)
o
(if
(= 1 x) (add1 x) x) results in 2 when x is 1 and 5 when x is 5
o
if
(= 1 x) (add1 x) ) is not allowed
·
cond: do something based upon a condition being met
o
(cond [ test true-do-expression
]
[ test-remainder
true-do-expression ]
[ else else-do-expression
] )
o
(cond [ (= 1 x) (add1 x) ]
[ (= 2 x) (+ x 4) ]
[ else (+ x 6 ) ] )
results in 2
when x = 1; 6 when x = 2 and 13 when x = 7
Repetition
through recursion
·
add
from 1 to a max value
o
(define
(addnum i) (cond [ ( <= i 0) 0 ] [ else ( + i (addnum (- i 1)))]))
o
(addnum 5) gives 15
·
add
a list of numbers
(define (addlist
mylist2)
(cond [ (eq? mylist2 null)
0]
[ else ( + (first
mylist2 )
(addlist (drop mylist2 1)))]))
(addlist mylist)Macros
Add new
commands
·
syntax:
(define-syntax-rule (ruleName parm1 parm2 ..)
(definition) )
·
(define-syntax-rule
(swap! x y) (let ([temp x]) (set! x y) (set! y temp)))
·
(define
x 3) (define y 4) (swap! x y) x y will print 4 3
Input / Output
·
A
port is similar to a file descriptor in unix
·
open
a port to connect to a new file: (define out-port (open-output-file "myfolder/tmp.txt"))
·
open
a port to connect to an existing file in prep for appending: (define out-port
(open-output-file "myfolder/tmp.txt" #:exists
'append))
·
open
a port to connect to an existing file to read it: (define in-port
(open-input-file "myfolder/tmp.txt"))
·
read
from an open file : (displayln (read-line in-port))
·
write
to the open file: (displayln "hi" out-port)
·
close
the output file : (close-output-port out-port)
·
close
the input file: (close-input-port in-port)
Modules:
·
create
it with (module moduleName itsBase)
example (module cake racket/base)
·
list
functions the importer can use with (provide functionName)
example (provide bake-cake)
o
no
indication of parameters needed
·
To
import a module : (require ModuleName) example
(require picturing-programs)
Pictures (must
add (require
picturing-programs)
·
make
filled rectangle: (rectangle length width fillMode
color) ex: (rectangle 10 30 "outline" "red") or (rectangle 10 30 "solid"
"black")
·
make
circle: (circle radius fillMode
color) ex: (circle 30 "solid" "yellow")
·
make
picture of text: (text string font color) ex: (text "words to become pic" 12
"black")
·
place
two pictures one under the other: (above pic1 pic2) ex: (above (circle 30)
(circle 50))
·
place
two pictures next to each other: (beside pic1 pic2) ex: (beside (circle 30) (circle 50))
·
place
one picture on top of another: (overlay overlayPic basePic) ex: (overlay (circle 30 "solid"
"red") (circle 50 "outline" "black"))
·
place
one picture on top at a certain point (overlay/xy overlayPic x y basePic) ex: (overlay/xy (circle 30 "solid" "red") 30 10 (circle 50 "outline"
"black"))
Construct for
reading through a text file:
·
Create
a function that handles one line from the file. Either split the line using a
list created from the line ( string-split) or remove
the end of line characters (string-replace) and use the entire line as a string.
To use pictures, remember (require picturing-programs)
(define ( handleline
line pic)
(let*
([ mystrings (string-split line ",")]
[
size (first mystrings) ]
[
color (list-ref mystrings 1)]
[ mytext (list-ref mystrings
2)])
(above
(above pic
(
square (string->number size) "solid" color))
(text mytext 12 color)
; (text (string-replace
line "\r" "")))
)))
·
Create
a file reading function that reads a line from a file and tests whether the
line is an end of file marker. If it is, print your result, otherwise, call
your file reading function recursively, passing it the function that handled
the line.
(define
in-port (open-input-file "c:/junk/tea.txt"))
(define
(filefunc pic)
( let
( [theline (read-line in-port
)] )
( cond [ (eof-object? theline ) pic]
[ else (filefunc (handleline theline pic ))
])))
·
Here
is sample data for my example:
#| sample data in tea.txt
10,red,10red,
15,blue,15blue,
|#
Much of this guide is a rework of http://learnxinyminutes.com/docs/racket/