learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-5.scm (1656B)


      1 (define-library (sicp solutions chapter-4 exercise-5)
      2   (import (schemb base)
      3           (ice-9 match))
      4 
      5   (begin
      6     (define (eval exp env)
      7       (cond ((self-evaluating? exp) exp)
      8             ((variable? exp) (lookup-variable-value exp env))
      9             ((quoted? exp) (text-of-quotation exp))
     10             ((assignment? exp) (eval-assignment exp env))
     11             ((definition? exp) (eval-definition exp env))
     12             ((if? exp) (eval-if exp env))
     13             ((lambda? exp)
     14              (make-procedure (lambda-parameters exp)
     15                              (lambda-body exp)
     16                              env))
     17             ((begin? exp)
     18              (eval-sequence (begin-actions exp) env))
     19             ((cond? exp) (eval (cond->if exp) env))
     20             ((application? exp)
     21              (apply (eval (operator exp) env)
     22                     (list-of-values (operands exp) env)))
     23             (else
     24              (error "Unknown expression type -- EVAL" exp))))
     25 
     26     (define (cond->if exp)
     27       (let expand-clauses ((clauses (cdr exp)))
     28         (match clauses
     29           ((cond) 'false)
     30           (`((else ,actions)) (sequence->exp actions))
     31           (`((else ,actions) ,rest ...)
     32            (error "ELSE clause isn't last -- COND->IF"
     33                   clauses))
     34           (`((,predicate ,actions) ,rest ...)
     35            (make-if predicate
     36                     (sequence->exp actions)
     37                     (expand-clauses '())))
     38           (`((,predicate => ,recipient) ,rest ...)
     39            (let ((predicate-value (eval predicate)))
     40              (make-if predicate-value
     41                       (recipient predicate-value)
     42                       rest))))))))