functional programming - Can functions with non-pure arguments of type functions be pure? -
can functions take non-pure functions arguments considered pure if not change/store state directly, don't reference global variables etc? , how draw line on pure , not, solely on function's in question code or take account effects of invoking arguments?
e.g. imagine scenario, pure function represents stateless workflow , takes arguments few actions executed during workflow. 1 of these actions changes state somewhere. if i'm looking strictly @ implementation of workflow, seems pure, modify state invoking parameter function modifies state. i'm tempted speculate workflow non-pure, passing in different argument not change state make pure, i'm confused.
any appreciated. thank you.
(define (its-not-me launch-rockets) (lambda () (launch-rockets)))
is indeed pure. doesn't launch rockets, constructs computation will, if called. no side effects caused merely constructing such computation.
(define (it-is-me launch-rockets) (launch-rockets) ((its-not-me launch-rockets)))
does indeed launches rockets, twice. whether directly or indirectly, doesn't matter. if causes side effects during execution, causes side effects during execution.
and if have
(define (launch-rockets) (if (prime? (random-integer)) (do-actually-launch-rockets) (list 'do-nothing)))
nothing changes. first still pure, constructing computation potentially causing side-effects, impure.
even
(define (launch-rockets) (if (prime? (random-integer)) (list 'i-am-only) (list 'kidding)))
isn't pure function, uses non-pure effect, randomness. pure functions return same result same arguments.
Comments
Post a Comment