Ah, I should've said that performance is not ideal with either compose (or
thrush), since they build the entire e.g. 10 million long function chain
before calling it, so tail-recursive variants below
(define (iterate f n x)
(for/fold ([x x])
([i n])
(f x)))
or with the thread-th
There’s also the thrush function from the point-free package, which uses the
argument order of the threading macro while providing the functional style of
compose.
http://pkg-build.racket-lang.org/doc/point-free/index.html?q=thrush#%28def._%28%28lib._point-free%2Fmain..rkt%29._thrush%29%29
If "thread-through macro" refers to "
http://www.greghendershott.com/2013/05/the-threading-macro.html"; then I
recommend a functional alternative called "compose":
(define print/cdr
(match-lambda
[(cons x xs) (print x) xs]))
(define print-two
(compose print/cdr print/cdr))
The result is
How about
(define print-two
(match-lambda
[(list* a b rest)
(print a)
(print b)
rest]))
?
On 03/12/2015 06:25 PM, Don Green wrote:
> ;Design A:
> ;Rating: 1 out of 10
> ;Poor because it uses set!
> (define print-two
> (lambda (f)
>(print (first f))
>(set! f (rest f
racket-lang.org] On Behalf Of Don Green
Sent: jueves, 12 de marzo de 2015 19:25
To: users@racket-lang.org
Subject: [racket] Looking for better designs to learn principles.
;Design A:
;Rating: 1 out of 10
;Poor because it uses set!
(define print-two
(lambda (f)
(print (first f))
;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
6 matches
Mail list logo