{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw1 - CS 60A Solution to homework#1 Fall 1993 1-5 didn't...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 60A -- Solution to homework #1 Fall 1993 1-5 didn't require any original thinking. 6. There are, of course, many possible ways to write this. None is perfectly elegant. The difficulty is figuring out which of the three arguments is smallest, so you can leave it out of the computation. Here is a straightforward solution: (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (cond ((and (<= a b) (<= a c)) (sumsq b c)) ((and (<= b a) (<= b c)) (sumsq a c)) (else (sumsq a b)) )) Some additional thought suggests that some of the testing is unnecessary (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (cond ((and (<= a b) (<= a c)) (sumsq b c)) ;; if we continue to here, a is not the smallest of a b c so ((<= b c) (sumsq a c)) (else (sumsq a b)) )) or even (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (if (and (<= a b) (<= a c)) (sumsq b c) (sumsq a (max b c)))) If you didn't think of using AND to identify the conditions, it could also be done using nested IFs: (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (if (>= a b) (if (>= b c) (sumsq a b) (sumsq a c)) (if (>= a c) (sumsq a b) (sumsq b c)))) Some people want to start by solving a subproblem: a function to find the two largest numbers. This can be done, but it's harder: (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq nums) (+ (square (first nums)) (square (last nums)))) (define (two-largest a b c)
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
(cond ((and (<= a b) (<= a c)) (sentence b c)) ((and (<= b a) (<= b c)) (sentence a c)) (else (sentence a b)))) (sumsq (two-largest a b c))) The trick here is that a function can't return two values, so two-largest has to return a sentence containing the two numbers. This hardly seems worth the effort, but the attempt to split the problem into logical pieces was well-motivated. It's a good idea in general, but it didn't work out well this time. The following solution wins the cuteness award, but it's a bit tricky: (define (sum-square-large papa mama baby) (define (square x) (* x x)) (cond ((> mama papa) (sum-square-large mama papa baby)) ((> baby papa) (sum-square-large baby papa mama)) ((> baby mama) (sum-square-large papa baby mama)) (else (+ (square papa) (square mama))))) Maybe the simplest way is to use min, a built-in scheme function ... (define (sum-square-large a b c) (define (square x) (* x x)) (define (sumsq x y) (+ (square x) (square y))) (cond ((equal? a (min a b c)) (sumsq b c)) ((equal? b (min a b c)) (sumsq a c)) (else (sumsq a b)) )) 7. This is an open-ended problem (child, children; alumnus, alumni) but here's a version that does some words correctly: (define (plural wd) (cond ((member? (last wd) '(o s x)) (word wd 'es)) ((and (equal? (last wd) 'y) (not (vowel? (last (bl wd))))) (word (bl wd) 'ies)) (else (word wd 's)) )) If that consonant-then-y test looks too messy for you, it can be
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern