ProgrammingLanguagesB Flashcards

1
Q

Why do you need to write #lang racket at the top of all your Racket files?

A

It is so DrRacket will interpret the code as Racket code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Simple racket file

A

lang racket

(provide (all-defined-out))

(define s “hello”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define variable x and assign it 3

A

(define x 3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Define variable y and assign it x+5

A

(define y (+ x 5))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Define function that gets number and returns it’s square

A

(define square1
(lambda (x)
(* x x)))

; OR

(define (square1 x) (* x x))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example of pow function(RECURSIVE)

A

(define (pow1 x y)
(if (= y 0)
1
(* x (pow1 x (- y 1)))))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Example of Currying

A

(define pow2
(lambda (x)
(lambda (y)
(pow1 x y))))

(define three-to-the (pow2 3))
(three-to-the 5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List processing

A
;Empty list: null
;Cons constructor: cons
;Access head of list: car
;Access tail of list: cdr
;Check for empty: NULL?
;Lists without cons: (list a1 a2 a3 a4 ... an)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Racket syntax. A term is either a:

A
  • an atom, e.g. #t, #f, 34, “hi”, null, 4.0, x…
  • a special form, e.g., define, lambda, if
  • a sequence of terms in parens: (t1 t2 … tn)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Cond operator syntax

A
(cond [e1a e1b]
           [e2a e2b]
           [e3a e3b]
                  ...
           [eNa eNb])
  • Good style: eNa should be #t
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

True and False in Racket

A
#t - True
#f - False
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Local bindings in Racket

A

let
let*
letrec
define

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Let

A

can bind any number of local variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Let example

A

(define (silly-double x)
(let ([x (+ x 3)]
[y (+ x 2)])
(+ x y -5)))

;> (silly-double 2)
;9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Let*

A

The expressions are evaluated in the environment produced from previous bindings

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Let* example

A

(define (silly-double-2 x)
(let* ([x (+ x 3)]
[y (+ x 2)])
(+ x y)))

;> (silly-double-2 2)
;12

17
Q

Letrec

A

the expressions are evaluated in the environment that includes all the bindings. Needed for mutual recursion

18
Q

Letrec example

A
(define (silly-triple x)
  (letrec ([y (+ x 2)]
           [f (lambda(z) (+ z y w x))]
           [w (+ x 7)])
    (f -9)))
19
Q

Mutation with !set

A

(set! x 45) ; changes value of x

20
Q

Type of lists

A

Proper: (cons 1 (cons 2 (cons 3 null)))
Improper: (cons 1 (cons 2 3))

21
Q

mcons

A

mutable cons

22
Q

mcons example

A
> (define y (mcons 14 null))
> (define z y)
> (mcar y)
14
> (set-mcar! y 47)
> y
(mcons 47 '())
> z
(mcons 47 '())
23
Q

mcons and proper-list

A

mcons cannot be a proper list

24
Q

Thunk

A

A zero-argument function used to delay evaluation

25
Thunk example
(define (my-if x y z) (if x (y) (z))) (define (fact n) (my-if (= n 0) (lambda() 1) (lambda() (* n (fact (- n 1))))))
26
Streams
A stream is an infinite sequence of values
27
Streams example
; 1 1 1 1 1 1 1 (define ones (lambda () (cons 1 ones))) ; 1 2 3 4 5 (define (f x) (cons x (lambda () (f (+ x 1))))) ; using (car (f 5)) ; 5 (car ((cdr (f 5)))) ; 6
28
Memoization
Hold values in function for already calculated results
29
Memoization example
(define fibonacci3 (letrec([memo null] [f (lambda (x) (let ([ans (assoc x memo)]) (if ans (cdr ans) (let ([new-ans (if (or (= x 1) (= x 2)) 1 (+ (f (- x 1)) (f (- x 2))))]) (begin (set! memo (cons (cons x new-ans) memo)) new-ans)))))]) f))
30
Macros define-syntax
(define-syntax mif (syntax-rules (then else) [(mif e1 then e2 else e3) (if e1 e2 e3)]))
31
what did (struct foo (bar baz quux) #:transparent)?
``` ; defines a new kind of thing and introduce ; several new functions (foo e1 e2 e3) ; return "a foo" with ; bar, baz, quux fields holding results ; of evaluating e1, e2 and e3 (foo? e) ; evaluates e and returns #t ; if and only if the result is something ; that was made with the foo function (foo-bar e) (foo-baz e) (foo-quux e) ``` ``` #:transparent is an optional attribute on struct definitions - for us, prints struct values in the REPL rather then hiding them which is convenient for debugging homework #:mutable - provides more functions - can decide if each struct supports mutation, with usual advantages and disadvantages - mcons is just predefined mutable struct ```