Docsity

Introduction to Racket Programming Language, Lecture notes of Programming Languages

Partial preview of the text

Download Introduction to Racket Programming Language and more Lecture notes Programming Languages in PDF only on Docsity!

CSE341: Programming Languages

Lecture 13

Racket Introduction

Dan Grossman

Spring 2016

Racket

Next two units will use the Racket language (not ML) and the DrRacket programming environment (not Emacs)

  • Installation / basic usage instructions on course website
  • Like ML, functional focus with imperative features
  • Anonymous functions, closures, no return statement, etc.
  • But we will not use pattern-matching
  • Unlike ML, no static type system: accepts more programs, but most errors do not occur until run-time
  • Really minimalist syntax
  • Advanced features like macros, modules, quoting/eval, continuations, contracts, …
  • Will do only a couple of these

Getting started

DrRacket “definitions window” and “interactions window” very similar to how we used Emacs and a REPL, but more user-friendly

  • DrRacket has always focused on good-for-teaching
  • See usage notes for how to use REPL, testing files, etc.
  • Easy to learn to use on your own, but lecture demos will help

Free, well-written documentation:

  • http://racket-lang.org/
  • The Racket Guide especially, http://docs.racket-lang.org/guide/index.html

File structure

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

Some niceties

Many built-in functions (a.k.a. procedures) take any number of args

  • Yes * is just a function
  • Yes you can define your own variable-arity functions (not shown here)

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

An old friend: currying

Currying is an idiom that works in any language with closures

  • Less common in Racket because it has real multiple args

(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 …

Examples

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

Racket syntax

Ignoring a few “bells and whistles,”

Racket has an amazingly simple syntax

A term (anything in the language) is either:

  • An atom , e.g., #t, #f, 34 , "hi", null, 4.0, x, …
  • A special form , e.g., define, lambda, if
    • Macros will let us define our own
  • A sequence of terms in parens: (t1 t2 … tn)
    • If t1 a special form, semantics of sequence is special
    • Else a function call
  • Example: (+ 3 (car xs))
  • Example: (lambda (x) (if x "hi" #t))

Why is this good?

By parenthesizing everything, converting the program text into a tree representing the program ( parsing ) is trivial and unambiguous

  • Atoms are leaves
  • Sequences are nodes with elements as children
  • (No other rules)

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

Parenthesis bias

  • If you look at the HTML for a web page, it takes the same approach: - (foo written - ) written
  • But for some reason, LISP/Scheme/Racket is the target of subjective parenthesis-bashing - Bizarrely, often by people who have no problem with HTML - You are entitled to your opinion about syntax, but a good historian wouldn’t refuse to study a country where he/she didn’t like people’s accents

Parentheses matter

You must break yourself of one habit for Racket:

  • Do not add/remove parens because you feel like it
    • Parens are never optional or meaningless!!!
  • In most places (e) means call e with zero arguments
  • So ((e)) means call e with zero arguments and call the result with zero arguments

Without static typing, often get hard-to-diagnose run-time errors

Examples (more in code)

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

Example

(define (sum xs) (if (null? xs) 0 (if (number? (car xs)) (+ (car xs) (sum (cdr xs))) (+ (sum (car xs)) (sum (cdr xs))))))

  • No need for a fancy datatype binding, constructors, etc.
  • Works no matter how deep the lists go
  • But assumes each element is a list or a number
    • Will get a run-time error if anything else is encountered

Better style

Avoid nested if-expressions when you can use cond-expressions instead

  • Can think of one as sugar for the other

General syntax: (cond [e1a e1b]

[e2a e2b] … [eNa eNb])

  • Good style: eNa should be #t