Functional Problem Solving (CSC 151 2014S) : EBoards

CSC151.02 2014S, Class 20: Naming Local Values


Overview

Preliminaries

Admin

Upcoming Work.

Pre-quiz Questions

What are the differences between if and cond?

If asks you to choose between two options, cond lets you do as many as you want.

Nesting ifs lets you achieve the "as many as you want

(if (test1)
    consequent1
    (if (test2)
        consequent2
        (if (test3)
            consequent3
            alternate)))

(cond
  [(test1) consequent1]
  [(test2) consequent2]
  [(test3) consequent3]
  [else alternate])

For choosing between two things, most people find the if easier. For more than two choices, most people find cond easier.

Another advantage to cond: Multiple consequents

(cond
  [(test1) consequent1a consequent1b]
  [(test2) consequent2a consequent2b consequent2c]
  [(test3) consequent3]
  [else alternate])

When is when you have one test, several consequents, no alternate.

Quiz

Lab

Note: Scheme likes parentheses, so

(let ((NAME VAL))
  EXP)

Alternately

(let ([NAME VAL])
  EXP)

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-2014 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.