functional programming - Destructive operations in scheme environments -
i'm confused destructive operations in scheme. let's have list , destructive procedures defined in global environment:
(define '(a b c)) (define (mutate-obj x) (set! x '(mutated))) (define (mutate-car! x) (set-car! x 'mutated)) (define (mutate-cdr! x) (set-cdr! x 'mutated))
then have following expression evaulation:
(mutate-obj! a) => (a b c) (mutate-car! a) => (mutated b c) (mutate-cdr! a) => (mutated . mutated)
why isn't set!
having effect on a
outside procedure when both set-car!
, set-cdr!
have? why isn't expression on first line evaluating (mutated)
? how of work?
the first example isn't working imagined. although both x
parameter , a
global variable pointing same list, when execute (set! x '(mutated))
set x
(a parameter local procedure) point different list, , a
remains unchanged. it'd different if wrote this:
(define (mutate-obj) (set! '(mutated)))
now a
gets mutated inside procedure. second , third procedures modifying contents of a
list, pointed x
, change gets reflected "outside" once procedure returns.
Comments
Post a Comment