Functional Problem Solving (CSC 151 2015F) : EBoards

CSC151 2015F, Review Session Week 08 - 2015-10-29


Overview

About the Quiz

Topics

Some things to know about

Types of Questions

Sample Quiz Questions

There are enough of these that you should check the eboard.

Implement a procedure

Without using list-ref, write a procedure, (my-list-ref lst n), that extracts element n of a list. For example,

> (my-list-ref (list "red" "orange" "yellow" "green" "blue" "indigo" "violet") 5)
"indigo"
> (my-list-ref (list "red" "orange" "yellow" "green" "blue" "indigo" "violet") 0)
"red"

A Solution

Find the errors

Identify the errors in the following procedure, which is supposed to count the number of odd values in a list of integers.

(define tally-odd
  (lambda (lst)
    (tally-odd-kernel 0 lst)))

(define tally-odd-helper
  (lambda (lst tally)
    (cond
      [(null? lst)
       0]
      [(odd? (car lst))
       (+ 1 tally)]
      [else
       (tally-odd helper lst tally)])))

A Solution

Show the steps

Consider the following procedure

(define fun
  (lambda (lst)
    (if (null? lst)
        0
        (- (car lst) (fun (cdr lst))))))

Show the steps in evaluating (fun '(1 2 3 4 5)). You need not show the evaluation of if, car, and cdr; just show the results. For example,

(fun '(1 2 3 4 5))
=> (- 1 (fun '(2 3 4 5)))

Show the steps

Consider the following procedure

(define fun
  (lambda (lst)
    (funner (cdr lst) (car lst))))

(define funner
  (lambda (lst val)
    (if (null? lst)
        val
        (funner (cdr lst) (- val (car lst))))))

Show the steps in evaluatng (fun '(1 2 3 4 5). You can immediately evaluate car, cdr, and if.

Complete the code

Finish the instructions for the tally-odd procedure below.

(define tally-odd
  (lambda (lst)
    (cond
      [(null? lst)
       _____]
      [(odd? (car lst))
       _____ (tally-odd (cdr lst))]
      [else
       _____ (tally-odd (cdr lst))])))

A solution

(define tally-odd
  (lambda (lst)
    (cond
      [(null? lst)
       0]
      [(odd? (car lst))
       (+ 1 (tally-odd (cdr lst)))]
      [else
       (tally-odd (cdr lst))])))

Would also accept the last line as the following. (But it's somewhat silly to add 0, it's just extra code and extra work.)

       (+ 0 (tally-odd (cdr lst)))])))

Document

Fill in the Six P's for the following procedure

(define whatzitdo
  (lambda (lst)
    (whatzitdotwo lst null)))

(define whatzitdotwo
  (lambda (lst val)
    (if (null? lst)
        val
        (whatzitdotwo (cdr lst) (cons (car lst) val)))))

Evaluate

Consider the following procedure

(define whatzitdo
  (lambda (lst)
    (whatzitdotwo lst null null)))

(define whatzitdotwo
  (lambda (lst foo bar)
    (if (null? lst)
        (list (reverse foo) (reverse bar))
        (whatzitdotwo (cdr lst)
                      (cons (car lst) bar)
                      foo))))

What is the value of (whatzitdo '(1 2 3 4 5))?

A Solution

(whatzitdo '(1 2 3 4 5))
=> (w2 '(1 2 3 4 5) '() '())
=> (w2 '(2 3 4 5) '(1) '())
=> (w2 '(3 4 5) '(2) '(1))
=> (w2 '(4 5) '(3 1) '(2))
=> (w2 '(5) '(4 2) '(3 1))
=> (w2 '() '(5 3 1) '(4 2))
=> '((1 3 5) (2 4))
; It split the list in half, using alternating positions!

Show steps

Consider the following procedure.

(define fun (lambda (a b) (if (> a b) null (cons a (fun (+ a 1) b)))))

Show the steps in evaluating (fun 5 9).

A Solution

(fun 5 9) (cons 5 (fun 6 9)) (cons 5 (cons 6 (fun 7 9))) (cons 5 (cons 6 (cons 7 (fun 8 9)))) (cons 5 (cons 6 (cons 7 (cons 8 (fun 9 9))))) (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 (fun 10 9)))))) (cons 5 (cons 6 (cons 7 (cons 8 (cons 9 null))))) (cons 5 (cons 6 (cons 7 (cons 8 '(9))))) (cons 5 (cons 6 (cons 7 '(8 9)))) (cons 5 (cons 6 '(7 8 9)))) (cons 5 '(6 7 8 9)) '(5 6 7 8 9))

Evaluate

Consider the following procedure.

(define fun (lambda (a b) (if (> a b) null (cons a (fun (+ a 1) b)))))

What is the result of (fun -3 8)?

A Solution

'(-3 -2 -1 0 1 2 3 4 5 6 7 8)

Document

Document the following procedure, which takes two integers as input.

(define fun (lambda (a b) (if (> a b) null (cons a (fun (+ a 1) b)))))