Functional Problem Solving (CSC 151 2013F) : EBoards

CSC151.02 2013F, Class 28: Preconditions, Revisited


Overview

Admin

Finding the Largest Element

(define largest
  (lambda (lst)
    (if (null? (cdr lst))
        (car lst)
        (max (car lst) (largest (cdr lst))))))

Helper recursion; Keep track of intermediate result (typcially "best solution so far" or "partial computation") and remaining values vs.

(define largest
  (lambda (lst)
    (largest-helper (car lst) (cdr lst))))

(define largest-helper
  (lambda (largest-so-far remaining)
    (if (null? remaining)
        largest-so-far
        (largest-helper (max largest-so-far (car remaining))
                        (cdr remaining)))))

vs.

(define largest-helper
  (lambda (largest-so-far remaining)
    (if (null? remaining)
        largest-so-far
        (if (> (car remaining) largest-so-far)
            (largest-helper (car remaining) (cdr remaining))
            (largest-helper largest-so-far (cdr remaining))))))

vs.

(define largest-helper
  (lambda (largest-so-far remaining)
    (if (null? remaining)
        largest-so-far
        (largest-helper (if (> (car remaining) largest-so-far)
                            (car remaining)
                            largest-so-far)
                        (cdr remaining)))))

vs.

 (define largest-helper
   (lambda (largest-so-far remaining)
     (cond
       [(null? remaining) 
        largest-so-far]
       [(> (car remaining) largest-so-far)
        (largest-helper (car remaining) (cdr remaining))]
       [else
        (largest-helper largest-so-far (cdr remaining))])))

Thinking About Helper Recursion

"Count the number of color names that include the word "green""

(helper 0 '("red" "green" "blue" "lightgreen" "yellow" "greenish"))
(helper 0 '("green" "blue" "lightgreen" "yellow" "greenish"))
(helper 1 '("blue" "lightgreen" "yellow" "greenish"))
(helper 1 '("lightgreen" "yellow" "greenish"))
(helper 2 '("yellow" "greenish"))

If the car of the list includes the word green, add 1 to the counter If the car of the list does not incldue the word green, don't change the counter

(define helper
   (lambda (count remaining)
      (cond
        [(null remaining) count]
        [(string-contains? (car remaining) "green")
         (helper (+ 1 count) (cdr remaining))]
        [else (helper count (cdr remaining))])))

Verifying preconditions

The error procedure

(define helpful-largest (lambda (lst) (if (null? lst) (error "helpful-largest expects a nonempty list") (if (null? (cdr lst)) (car lst) (max (car lst) (helpful-largest (cdr lst)))))))

Good programming style: Predict likely incorrect input and tell user about it

Husk and Kernel programming

Lab


Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-2013 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.)

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.