


































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A lecture note on Racket programming language. It covers the basic syntax, file structure, and examples of Racket. the differences between Racket and ML, and the features of Racket such as macros, modules, quoting/eval, continuations, and contracts. The document also provides examples of built-in functions and non-anonymous function definitions. The document emphasizes the importance of parentheses in Racket and provides examples of common errors. useful as study notes and lecture notes for a programming languages course.
Typology: Lecture notes
1 / 42
This page cannot be seen from the preview
Don't miss anything!



































Next two units will use the Racket language (not ML) and the DrRacket programming environment (not Emacs)
DrRacket “definitions window” and “interactions window” very similar to how we used Emacs and a REPL, but more user-friendly
Free, well-written documentation:
Start every file with a line containing only
#lang racket
(Can have comments before this, but not code)
A file is a module containing a collection of definitions (bindings)…
Many built-in functions (a.k.a. procedures) take any number of args
Better style for non-anonymous function definitions (just sugar):
(define cube (lambda (x) (* x x x)))
(define (cube x) (* x x x))
(define (pow x y) (if (= y 0) 1 (* x (pow x (- y 1)))))
Currying is an idiom that works in any language with closures
(define pow (lambda (x) (lambda (y) (if (= y 0) 1 (* x ((pow x) (- y 1)))))))
(define three-to-the (pow 3)) (define eightyone (three-to-the 4)) (define sixteen ((pow 2) 4))
Sugar for defining curried functions:
(No sugar for calling curried functions)
(define ((pow x) y) (if …
(define (sum xs) (if (null? xs) 0 (+ (car xs) (sum (cdr xs)))))
(define (my-append xs ys) (if (null? xs) ys (cons (car xs) (my-append (cdr xs) ys))))
(define (my-map f xs) (if (null? xs) null (cons (f (car xs)) (my-map f (cdr xs)))))
Ignoring a few “bells and whistles,”
Racket has an amazingly simple syntax
A term (anything in the language) is either:
By parenthesizing everything, converting the program text into a tree representing the program ( parsing ) is trivial and unambiguous
Also makes indentation easy
Example:
No need to discuss “operator precedence” (e.g., x + y * z)
(define cube (lambda (x) (* x x x)))
define
cube lambda
x *
x x x
You must break yourself of one habit for Racket:
Without static typing, often get hard-to-diagnose run-time errors
Correct:
Treats 1 as a zero-argument function (run-time error):
Gives if 5 arguments (syntax error)
3 arguments to define (including (n)) (syntax error)
Treats n as a function, passing it * (run-time error)
(define (fact n)(if (= n 0) 1 (* n (fact (- n 1)))))
(define (fact n)(if (= n 0) (1)(* n (fact (- n 1)))))
(define (fact n)(if = n 0 1 (* n (fact (- n 1)))))
(define fact (n)(if (= n 0) 1 (* n (fact (- n 1)))))
(define (fact n)(if (= n 0) 1 (n * (fact (- n 1)))))
(define (sum xs) (if (null? xs) 0 (if (number? (car xs)) (+ (car xs) (sum (cdr xs))) (+ (sum (car xs)) (sum (cdr xs))))))
Avoid nested if-expressions when you can use cond-expressions instead
General syntax: (cond [e1a e1b]
[e2a e2b] … [eNa eNb])