




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Week 7 (05/17/19)
Installing scheme
We’ll be using racket for this class. Its a language in the Lisp-Scheme family. https://racket-lang.org/ References Racket Guide - https://docs.racket-lang.org/guide/index.html Racket Refer- ence - https://docs.racket-lang.org/reference/index.html Racket Cheat Sheet - https://docs.racket-lang.org/racket-cheat/index.html
What is scheme?
It is a LISP variant, (the second oldest high level programming language!) Features:
Basics
Identifiers - string with any chars except ( ) [ ] { } ” , ’ ; # |
ex: hello, string->int, number? Numbers ex: 1, 1/2, 0.
There are exact numbers and inexact numbers in scheme, and its kind of interesting to read about it if you’re feeling curious - https://docs.racket- lang.org/guide/numbers.html Booleans ex: #t, #f anything other than #f evaluates to true, so be careful. Strings “hello world” (display “hello world”) Predicates Type checking during run time! helpful predicates:
Arithmetic
(+ 3 5) -> 8 (- 1 1 1 1) -> - (* 3 5) -> 15 (/ 4 3) -> 4/ ( quotient 4 3) -> 1 # quotient floor the result
Lists
Lists in scheme are all linked lists ( list 1 2 3) -> '(1 2 3)
The empty list is ’() and proper lists end with the empty list.
helpful operators: length, append, reverse, member, empty?, list?, pair?
car and cdr
( car '(1 2 3)) -> 1 ( cdr '(1 2 3)) -> '(2 3)
Exercise:
( car '(2 3 4)) ->? # ( cdr '(2 3 4)) ->? #(3,4) ( cdr '(2. 4)) ->? #
Equivalence
equal?
( equal? '(1 2 3) '(1 2 3)) -> #t ( equal? '(a b c) '(d e f)) -> #f
=
(= 3 3) -> #t (= 500 1/2) -> #f (= 1 (* 3 1/3)) -> #t
eq?
( eq? '(1 2 3) '(1 2 3)) -> #f
( let ([x 5]) ( eq? x x)) -> #t
Other useful functions to note
and, or, not
e.g. if and finds something false, it immediately returns the false value. if or finds something true, it immediately returns the true value
e.g.
( and 1 2) -> 2 ( and 1 #f) -> #f ( or #f #f) -> #f ( or 1 #f) -> 1 (/ 1 0) -> division by zero
question
( or 1 (/ 1 0)) ->? # Define
we can also define functions that take an arbitrary number of arguments
Exercise write the doTwice function. (define (doTwice f x) (f (f x))) Lambdas
we can write anonymous functions in scheme
syntax: (lambda (
Lambda expression for arbitrary num of args There are functions in scheme that can take an arbitrary number of args ex: (list 1 2 3) -> '(1 2 3)
We can recreate this behavior!
syntax: (lambda
Exercise 2 write a factorial function (define factorial ( lambda (n fact) ( if (< n 2) fact (factorial (- n 1) (* fact n)))))
Exercise 3 write a function that checks for equality between two lists with the same length.
Quote Lists and programs are equivalent, we just decide whether or not to execute the code or treat it as data. (quote (+ 1 2)) -> '(+ 1 2)
( list '+ 1 2) -> '(+ 1 2) ( list (+ 1 2)) -> '(3) (the list of just 3)
Goal: Write a program to detect similarities between two Scheme programs expr-compare function takes two expressions and returns a new expression with similar parts combined
Variable % defines which program we want to execute
case 1 (expr-compare 12 12) -> 12 (expr-compare 12 20) -> ( if % 12 20) (expr-compare 'a '( cons a b)) -> ( if % a ( cons a b)) case 2
(expr-compare '( cons a b) '( cons a c)) -> ( cons a ( if % b c)) (expr-compare '( cons ( cons a b) ( cons b c)) '( cons ( cons a c) ( cons a c))) -> ( cons ( cons a ( if % b c)) ( cons ( if % b a) c)) case 3
(expr-compare '( list ) '( list a)) -> ( if % ( list ) ( list a)) (expr-compare '(quote (a b)) '(quote (a c))) -> ( if % '(a b) '(a c))
(expr-compare '( if x y z) '(g x y z)) -> ( if % ( if x y z) (g x y z))
case 4
(expr-compare '(( lambda (a) (f a)) 1) '(( (a) (g a)) 2)) -> (( (a) (( if % f g) a)) ( if % 1 2))
case 5
(expr-compare '(( lambda (a) a) c) '(( lambda (b) b) d)) -> (( lambda (a!b) a!b) ( if % c d))