CMPSC 461 - Scheme Language

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/16

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

17 Terms

1
New cards

Prefix Notation

4+(5*7) → (+4(* 5 7))

3+4 → (+ 3 4)

3*4 → (*3 4)

5+(2*2) → (+5(* 2 2))

2
New cards

Scheme Variables

foo = 3 → (define foo 3)

(* foo 4) = 12

3
New cards

Scheme Expressions

(E1 E2 … Ek)

E1 is the function to invoke

E2 … Ek Function arguments

4
New cards

User-Defined Functions

f(x) = x2 → (define (square x) (* x x))
(+ (square 2) (square 3)) = 4 + 9 = 13

f(x, y) = x + y2 → (define (f x y) (+ x (* y y))
(f 3 4) = 3 + 42 = 19

5
New cards

Built-int Functions (take 0+ params, apply operation to all params together)

(+ 2 3 5) = 11
(* 3 2 4) = 24
(+) = 0
(*) = 1
(+ 5) = 5
(* 8) = 9

6
New cards

Flow Control

(define (abs x) #Foo header)

(if (< x 0) #Conditional

(-x) #Built-in function

x)) #Return


(abs -3) = 3
(abs 3) = 3

7
New cards

List built-in functions

(sort (list 4 6 5)) = (4 5 6)
(sort (list 5 4 3 2 1) <) = (1 2 3 4 5)
(sort (list “abc” “a” “ab”) string<?) = (“a” “ab” “abc”)

(length (list 1 2)) = (1 2)

8
New cards

List manipulation

(define my-list (list 1 2 3 4 5))

my-list → (1 2 3 4 5)

(car my-list) → 1

(cdr my-list) → (2 3 4 5)

9
New cards

General info

Dynamic Typing
Functions = Values

10
New cards

Anonymous Functions (Lambda Calculus)

Small foos taking any amt of args, but only have one expression; Can only use once

(define square (lambda(x) (* x x)) = (define (square x) (* x x))

11
New cards

Parenthesis

Reserved for function call / invocation

(+ 3 4) is okay
(+ (3) 4) is not

(lambda(x) x) is okay
(lambda (x) (x)) is not

(lambda(x) (* x x)) is okay
(lambda (x) (* (x) x)) is not

12
New cards

Recursive Functions

(define diverge (lambda (x) (diverge (+ x 1))))

1) define foo
2) foo arg = lambda foo
3) lambda arg = fully defined foo

13
New cards

Booleans

#t → True
#f → False
number? → test whether argument is a number
equal? → test if values are equal (works with & beyond numbers)
= → reserved for numbers
and, or, not → they exist as operators

(equal? 2 2) → #t

(equal? x (* 2 y)) → does x = 2 * y ?

(equal? #t #t) → #t

(and (> 7 5) (< 10 20))) → (7 > 5) and (10 < 20) → #t

14
New cards

If expressions

(if P E1 E2) → If P then E1. Else, E2

(define (max x y) (if (> x y) x y) → if (x>y) return x. Else return y.

DOES NOT EVALUATE BOTH BRANCHES

15
New cards

Multi-Case Conditionals

(cond (P1 E1)
     (Pn En)
     (else En+1))

Ex:
(cond ((>= x 90) ‘A)

     ((>= x 80) ‘B)

     ((>= x 70) ‘C)

     ((>= x 60) ‘D)

     (else ‘F)))

16
New cards

Higher-Order Functions

Functions that: take foos as args & return foos as results. Ex: g(f, x) = f(f(x))

(define (twice f x) (f (f x)))
(define (PlusOne x) (+ 1 x))
(twice plusOne 2)
(twice square 2)
(twice (lambda (x) (+ x 2)) 3)

17
New cards

Let constructus

No variable can refer to another one defined in same let
(let ((x1 E1) (x2 E2) … (xk Ek)) E)

(let ((x (+ 3 2)) (* x x))) = (* (+ 3 2) (+ 3 2))

(let ((three-sq (square 3)) (four-sq (square 4))) (+ three -sq four-sq)) =
(+ (square 3) (square 4)


Variables can refer to earlier variables
(let* ((x 2) (y x)) y) = (let ((x 2)) (let ((y x)) y)

Ei can refer to themselves or each other
letrec ((x1 E1) (x2 E2) … (xk Ek)) E)
Here, scope of x1 is E1, E2, … and Ek and