首页 > 代码库 > scheme set-car! 模拟
scheme set-car! 模拟
#lang scheme
( define li ‘( 1 2 3 4 ) )
( define-syntax set-car!
( syntax-rules ()
( ( set-car! li elem )
( set! li ( cons elem ( cdr li ) ) ) ) ) )
( set-car! li ‘( a b ) )
或者
#lang scheme
( define ( s-cons x y )
( define ( set-x! v )
( set! x v ) )
( define ( set-y! v )
( set! y v ) )
( define ( dispatch m )
( cond [ ( eq? m ‘car ) x ]
[ ( eq? m ‘cdr ) y ]
[ ( eq? m ‘set-car! ) set-x! ]
[ ( eq? m ‘set-cdr! ) set-y! ]
[ else ( error "Undefined operation" m ) ] ) )
dispatch )
( define ( s-car z )
( z ‘car ) )
( define ( s-cdr z )
( z ‘cdr ) )
( define ( s-set-car! z val )
( ( z ‘set-car! ) val )
z )
( define ( s-set-cdr! z val )
( ( z ‘set-cdr! ) val )
z )
( define z ( s-cons 1 2 ) )
( s-car ( s-set-car! z 111 ) )
scheme set-car! 模拟