sicp

it2024-11-16  11

#lang racket ;;;;;;;;;;;;;;;;;;;;;;;;2.17 (define (last-pair L) (if (null? (cdr L)) L (last-pair (cdr L)))) ;;;;;;;;;;;;;;;;;;;;;;;2.18 (define (reverse L) (if (null? (cdr L)) L (cons (reverse (cdr L)) (car L)))) ;;;;;;;;;;;;;;;;;;;;;;2.19 (define us-coins (list 50 25 10 5 1)) (define us-coins2 (list 1 5 10 25 50)) (define uk-coins (list 100 50 20 10 5 2 1)) (define (cc amount coin-values) (cond ((= amount 0) 1) ((or (< amount 0) (no-more? coin-values)) 0) (else (+ (cc amount (except-first-denomination coin-values)) (cc (- amount (first-denomination coin-values)) coin-values))))) (define (no-more? coins) (null? coins)) (define (except-first-denomination coin-values) (cdr coin-values)) (define (first-denomination coins) (car coins)) ;;;;;;;;;;;;;;;;;;;;2.20 (define (same-parity sample . others) (filter (if (even? sample) even? odd?) (cons sample others))) ;;;;;;;;;;;;;;;;;;;;;;;test ;(last-pair (list 23 37 149 34)) ;(reverse (list 1 4 9 16 25)) ;(cc 100 us-coins) ;(cc 100 us-coins2) ;(same-parity 2 3 4 5 6 7)

 

转载于:https://www.cnblogs.com/tclan126/p/6390234.html

最新回复(0)