Docsity

CS131 Spring19 Discussion 1B: Scheme Basics and HW5 Walkthrough, Lecture notes of Functional Programming

Toggle sidebar
bg1
CS131, Spring19 – Discussion 1B
Week 7 (05/17/19)
Administrations
1. TA: Wenhao Zhang ([email protected])
2. Office hour:
• Time: 9:30am ~ 11:30am, Monday
• Location: 3rd floor, common area in Eng VI
3. Class Announcements
• All TAs slides are under resources tab on Piazza
• HW4 dued on this past Tuesday
• HW5 dued on 2019-05-23 (This coming Tuesday)
• Midterm is still being graded
• Please dont copy & paste code from stackoverflow or other sources.
4. Today’s agenda
• Scheme/Racket basics
• HW5 walkthrough
Scheme
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:
• functional programming language
• prefix syntax
• dynamically typed : values are typed, not variables -> runtime type check-
ing!
• the line between program and data is very very thin
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download CS131 Spring19 Discussion 1B: Scheme Basics and HW5 Walkthrough and more Lecture notes Functional Programming in PDF only on Docsity!

CS131, Spring19 – Discussion 1B

Week 7 (05/17/19)

Administrations

  1. TA: Wenhao Zhang ([email protected])
  2. Office hour:
    • Time: 9:30am ~ 11:30am, Monday
    • Location: 3rd floor, common area in Eng VI
  3. Class Announcements
    • All TAs slides are under resources tab on Piazza
    • HW4 dued on this past Tuesday
    • HW5 dued on 2019-05-23 (This coming Tuesday)
    • Midterm is still being graded
    • Please dont copy & paste code from stackoverflow or other sources.
  4. Today’s agenda
    • Scheme/Racket basics
    • HW5 walkthrough

Scheme

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:

  • functional programming language
  • prefix syntax
  • dynamically typed : values are typed, not variables -> runtime type check- ing!
  • the line between program and data is very very thin

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:

  • Numbers: number?, integer? rational? real?
  • Booleans: boolean?
  • Strings: string? e.g. ( boolean? #f) -> #t ( boolean? 1) -> #f

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 -> take first
  • cdr -> take rest

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

  • checks if its two arguments have the same value.

( equal? '(1 2 3) '(1 2 3)) -> #t ( equal? '(a b c) '(d e f)) -> #f

=

  • checks if two numbers are equal

(= 3 3) -> #t (= 500 1/2) -> #f (= 1 (* 3 1/3)) -> #t

eq?

  • checks if its two arguments are the same. pointer equality!

( eq? '(1 2 3) '(1 2 3)) -> #f

( let ([x 5]) ( eq? x x)) -> #t

Other useful functions to note

and, or, not

  • and & or use short circuit evaluation

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

  • Define variables (define )
  • This defines a variable. eg: (define e 2.71828) (define v (+ 1 2))
  • Define a function syntax: (define ( ) +) - define a function with 0 or more arguments - the final expr in the body is the return value eg: (define (timesTwo x) ( x 2)) (define (print_sth) ( display "hello world"))

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 (*) +) Exercise the syntax we used for defining functions is just syntatical sugar for lambdas. write doTwice using a lambda. (define doTwice ( lambda (f x) (f (f x)))) Lets call doTwice, (doTwice (lambda (x) (+ x 2)) 3)

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 +) (( lambda x x) 1 2 3) -> '(1 2 3)

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)

HW

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