* Difference letrec & environment binding
@ 2011-04-13 10:38 Hans Aberg
2011-04-13 11:51 ` dsmich
0 siblings, 1 reply; 2+ messages in thread
From: Hans Aberg @ 2011-04-13 10:38 UTC (permalink / raw)
To: guile-devel
What is the practical difference between the two ways of doing the same thing (letrec or an environment). Is letrec more efficient in the implementation of Guile?
Hans
(letrec (
(even? (lambda (n)
(if (zero? n) #t (odd? (- n 1)))))
(odd? (lambda (n)
(if (zero? n) #f (even? (- n 1))))))
(even? 1000))
((lambda ()
(define even? (lambda (n)
(if (zero? n) #t (odd? (- n 1)))))
(define odd? (lambda (n)
(if (zero? n) #f (even? (- n 1)))))
(even? 1000)))
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Difference letrec & environment binding
2011-04-13 10:38 Difference letrec & environment binding Hans Aberg
@ 2011-04-13 11:51 ` dsmich
0 siblings, 0 replies; 2+ messages in thread
From: dsmich @ 2011-04-13 11:51 UTC (permalink / raw)
To: guile-devel, Hans Aberg
---- Hans Aberg <haberg-1@telia.com> wrote:
> What is the practical difference between the two ways of doing the same thing (letrec or an environment). Is letrec more efficient in the implementation of Guile?
>
> Hans
>
>
> (letrec (
> (even? (lambda (n)
> (if (zero? n) #t (odd? (- n 1)))))
> (odd? (lambda (n)
> (if (zero? n) #f (even? (- n 1))))))
> (even? 1000))
>
> ((lambda ()
> (define even? (lambda (n)
> (if (zero? n) #t (odd? (- n 1)))))
> (define odd? (lambda (n)
> (if (zero? n) #f (even? (- n 1)))))
> (even? 1000)))
>
No difference at all. Those generate identical code. I believe the second form is transformed into the first during compilation.
scheme@(guile-user)> ,x a
Disassembly of #<procedure a ()>:
0 (assert-nargs-ee/locals 8)
2 (br :L204) ;; -> 25 at (unknown file):15:9
6 (local-ref 0) ;; `n'
8 (make-int8:0) ;; 0 at (unknown file):16:8
9 (ee?)
10 (br-if-not :L205) ;; -> 16 at (unknown file):16:4
14 (make-true) ;; #t
15 (return)
16 (local-ref 0) ;; `n'
18 (sub1) at (unknown file):16:27
19 (local-set 0) ;; `n'
21 (br :L206) ;; -> 29 at (unknown file):16:21
25 (br :L207) ;; -> 48 at (unknown file):17:8
29 (local-ref 0) ;; `n'
31 (make-int8:0) ;; 0 at (unknown file):18:8
32 (ee?)
33 (br-if-not :L208) ;; -> 39 at (unknown file):18:4
37 (make-false)
38 (return)
39 (local-ref 0) ;; `n'
41 (sub1) at (unknown file):18:28
42 (local-set 0) ;; `n'
44 (br :L209) ;; -> 6 at (unknown file):18:21
48 (make-int16 3 232) ;; 1000
51 (local-set 0)
53 (br :L209) ;; -> 6 at (unknown file):19:2
scheme@(guile-user)> ,x b
Disassembly of #<procedure b ()>:
0 (assert-nargs-ee/locals 8)
2 (br :L216) ;; -> 25 at (unknown file):23:16
6 (local-ref 0) ;; `n'
8 (make-int8:0) ;; 0 at (unknown file):24:8
9 (ee?)
10 (br-if-not :L217) ;; -> 16 at (unknown file):24:4
14 (make-true) ;; #t
15 (return)
16 (local-ref 0) ;; `n'
18 (sub1) at (unknown file):24:27
19 (local-set 0) ;; `n'
21 (br :L218) ;; -> 29 at (unknown file):24:21
25 (br :L219) ;; -> 48 at (unknown file):25:15
29 (local-ref 0) ;; `n'
31 (make-int8:0) ;; 0 at (unknown file):26:8
32 (ee?)
33 (br-if-not :L220) ;; -> 39 at (unknown file):26:4
37 (make-false)
38 (return)
39 (local-ref 0) ;; `n'
41 (sub1) at (unknown file):26:28
42 (local-set 0) ;; `n'
44 (br :L221) ;; -> 6 at (unknown file):26:21
48 (make-int16 3 232) ;; 1000
51 (local-set 0)
53 (br :L221) ;; -> 6 at (unknown file):27:2
-Dale
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-04-13 11:51 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-13 10:38 Difference letrec & environment binding Hans Aberg
2011-04-13 11:51 ` dsmich
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).