Created
November 14, 2013 23:03
-
-
Save yamanetoshi/7475945 to your computer and use it in GitHub Desktop.
EoPL Ex.2.19
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(add-load-path "../../define-datatype") | |
(load "define-datatype") | |
(define-datatype stack stack? | |
(empty-stack-record) | |
(extended-stack-record | |
(val scheme-value?) | |
(stk stack?))) | |
(define scheme-value? | |
(lambda (v) #t)) | |
(define empty-stack | |
(lambda () | |
(empty-stack-record))) | |
(define empty-stack? | |
(lambda (s) | |
(cases stack s | |
(empty-stack-record () #t) | |
(extended-stack-record (val stk) #f)))) | |
(define push | |
(lambda (value s) | |
(extended-stack-record value s))) | |
(define stack-access | |
(lambda (access s) | |
(cases stack s | |
(empty-stack-record () (eopl:error 'stack-access "No data")) | |
(extended-stack-record (val stk) | |
(cond ((eqv? access 'pop) | |
stk) | |
((eqv? access 'top) | |
val)))))) | |
(define pop | |
(lambda (s) (stack-access 'pop s))) | |
(define top | |
(lambda (s) (stack-access 'top s))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment