Don Green
2015-03-12 18:25:21 UTC
;Design A:
;Rating: 1 out of 10
;Poor because it uses set!
(define print-two
(lambda (f)
(print (first f))
(set! f (rest f))
(print (first f))
(set! f (rest f))
f))
(void (print-two '(1 2))) ;=> 12
;-------------------------
;Design B:
;Rating: 2 out of 10
;Better because it nests expressions to avoid using set!
;Poor because it less readable.
(define print-two
(lambda (f)
(print (first f))
(print (first (rest f)))
f))
(void (print-two '(1 2))) ;=> 12
When called in situations that allow one expression only, enclose call
within a 'begin' expression.
;-------------------------
;Design C:
;Rating: 3 out of 10
;Is this an even better design because it is more readable than nesting
expressions as in Design B above?
(define (print-two f)
(let* ([_ (print (first f))]
[f (rest f)]
[_ (print (first f))]
[f (rest f)])
f))
(void (print-two '(1 2))) ;=> 12
;-------------------------
My questions are:
"Can you recommend a better method?"
"Can you recommend a better method using 'define with lambda'?"
"Does your better method use a macro?"
"Does your better method use a thread-through macro?" If so, could you
please provide the definition of the thread-through macro.
THANKS!
;Rating: 1 out of 10
;Poor because it uses set!
(define print-two
(lambda (f)
(print (first f))
(set! f (rest f))
(print (first f))
(set! f (rest f))
f))
(void (print-two '(1 2))) ;=> 12
;-------------------------
;Design B:
;Rating: 2 out of 10
;Better because it nests expressions to avoid using set!
;Poor because it less readable.
(define print-two
(lambda (f)
(print (first f))
(print (first (rest f)))
f))
(void (print-two '(1 2))) ;=> 12
When called in situations that allow one expression only, enclose call
within a 'begin' expression.
;-------------------------
;Design C:
;Rating: 3 out of 10
;Is this an even better design because it is more readable than nesting
expressions as in Design B above?
(define (print-two f)
(let* ([_ (print (first f))]
[f (rest f)]
[_ (print (first f))]
[f (rest f)])
f))
(void (print-two '(1 2))) ;=> 12
;-------------------------
My questions are:
"Can you recommend a better method?"
"Can you recommend a better method using 'define with lambda'?"
"Does your better method use a macro?"
"Does your better method use a thread-through macro?" If so, could you
please provide the definition of the thread-through macro.
THANKS!