Racket Flashcards

1
Q

Write a basic lambda the returns the second element in a list (assume there are more than 2 elms in list)?

A

((lambda (x) ;; declare params
(cadr x)) ;; expression
(list 1 2 3));; actual values

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

Define a function that takes list and a value then checks if all the values in the list are greater than the value?

A

(define (larger ls n)
(andmap
(lambda (x) (> x n))
ls))

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

Define a function that takes 2 ints as lower and upper bound (inclusive) and should return the largest square that is even?

A
(define (largestSquare x y)
  (apply max 
         (filter even? (drop 
                        (build-list (+ y 1) (lambda (x) (* x x)))
                        x))))

(largestSquare 1 10)

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

What is currying?

A

TBD

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

What is the general for of Let?

A
;; general form of let
(let ((name1 value1)
      (name2 value2)
      ...
      (nameN valueN))
   expression1
   expression2
   ...
   expressionQ)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the If form?

A

(if condition true_expression false_expression)

(if (= 0 1) (/ 1 0) (+ 2 3)) => 5
; note that the (/ 1 0) is not evaluated

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

Define a function that calculates the sum of a list?

A

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

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

What is a thunk?

A

Delay evaluation by putting an expression in a function!

– Thanks to closures, can use all the same variables later

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

Define a function that squares and returns odd numbers in a list?

A

(define (square-odd x)
(map (lambda (y) (* y y))
(filter odd? x)))

(square-odd (list 1 2 3 4 5))

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

Define a function that adds 1 to each element in a list.

A
(define (add1 x)
  (if (null? x)
      '()
      (cons (+ 1 (car x))
            (add1 (cdr x)))))

(add1 (list 1 2 3 4))

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

What is foldl?

A

Like map, foldl applies a procedure to the elements of one or more lists. Whereas map combines the return values into a list, foldl combines the return values in an arbitrary way that is determined by proc.

(foldl proc init lst)

> (foldl cons ‘() ‘(1 2 3 4))
‘(4 3 2 1)
(foldl + 0 ‘(1 2 3 4))
10

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

What does ZIP do?

A

Zipping combines two lists into a single list of pairs element-wise.

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

Define a myzip?

A
(define (myzip ls1 ls2)
  (cond 
    [(null? ls1) ls2]
    [(null? ls2) ls1]
    [else
      (cons (list (car ls1) (car ls2))
            (myzip (cdr ls1) (cdr ls2)))]))

(myzip ‘() ‘(a b c))
(myzip ‘(1 2 3) ‘())
(myzip ‘(1 2 3) ‘(a b c))

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

Define a myfoldl?

A
(define (myfoldl func init lst)
  (cond
    [(empty? lst) init]
    [else 
     (myfoldl func (func (car lst) init) (cdr lst))]))

(myfoldl cons ‘() ‘(a b c))
(myfoldl + 0 ‘(1 2 3))

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

What is the difference between eq? eqv? and equal?

A

eq? is pointer comparison.

eqv? is like eq? but does the right thing when
comparing numbers. For number use = to compare as it coercses into crest type

equal? returns true if its arguments have the same
structure.

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

Define a mymap?

A

(define (my-map f s)
(if (null? s)
‘()
(cons (f (car s)) (my-map f (cdr s)))))

17
Q

Define a myfilter?

A

(define (my-filter f s)
(cond ((null? s) ‘())
((f (car s)) (cons (car s) (my-filter f (cdr s))))
(else (my-filter f (cdr s)))))

18
Q

Define a sum?

A

(define (sum s)
(apply + s))

(sum ‘(1 2 3)) => 6

19
Q

What does apply do? And how to use for conditionals and there functions?

A

The apply function applies a function to a list of its
arguments.

Examples:
(apply even? ‘(3)) => #f
(apply + ‘(1 2 3 4)) => 10

20
Q

What is map and provide examples.

A

Applies proc to the elements of the lsts from the first elements to the last.

(map function list) ;; general form
(map null? ‘(3 () () 5)) => ‘(#f #t #t #f)
(map round ‘(3.3 4.6 5.9)) => ‘(3.0 5.0 6.0)
(map cdr ‘((1 2) (3 4) (5 6))) => ‘((2) (4) (6))
(map (lambda (x) (* 2 x)) ‘(3 4 5)) => ‘(6 8 10)

21
Q

What is filter and provide an example?

A

It applies a filter to the list.

filter (lambda (n) (> n 10)) ‘(5 10 15 20)) => ‘(15 20

22
Q

What is an issue with multiple lets and scopes?

A

For example, this doesn’t work, since x isn’t known
outside the body:

(let ((x 3) ;; this is variable declaration not body
(y (+ x 1))) ;; this is variable declaration not body
(+ x y)) ;; This is the body

To get around this problem, use let*:

(let* ((x 3)
(y (+ x 1)))
(+ x y))