"SICP Exercise" 149 Exercise 4.5

Source: Internet
Author: User

Exercise 4-5 Original

Exercise 4.5. Scheme allows an additional syntax for cond clauses, (= =). If evaluates to a true value, then is evaluated. Its value must is a procedure of one argument; This procedure was then invoked on the value of the, and the result is returned as the value of the the COND expression. For example

(cond ((assoc ‘b ‘((a 1) (b 2))) => cadr)           (else false))

Returns 2. Modify the handling of cond so, it supports this extended syntax.

Analysis

Code
 (define (extended-cond-syntax clause) (eq? ) (cadr clause) ' =])  (define (extended-cond-test clause) (car clause))  (define (extended-cond-recipient clause) (caddr clause) )  (define (cond->if expr)(expand-clauses (cond-clauses Expr           )))  (Define (expand-clauses clauses)          (if (null? clauses)‘false                  ( Let (First  (car clauses))(rest (cdr clauses) ))                           (Cond cond-else-clause?  first)   (if    (null?  rest)   ( sequence->exp  cond-actions  first) )   (error   "ELSE clause isn ' t last-- Cond->if " clauses) ) )                                     extended-cond-syntax?  first)   (make-if   (extended-cond-test  First"   (list    (extended-cond-recipient  first)  extended-cond-test  first) )   (expand-clauses  rest) ) )                                   (else (make-if (cond-predicate first) (sequence->exp (cond-actions first)) (expand-clauses rest))))))) 

"SICP Exercise" 149 Exercise 4.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.