unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: <dsmich@roadrunner.com>
To: guile-devel <guile-devel@gnu.org>, Hans Aberg <haberg-1@telia.com>
Subject: Re: Difference letrec & environment binding
Date: Wed, 13 Apr 2011 7:51:58 -0400	[thread overview]
Message-ID: <20110413115158.C5DYL.47361.root@cdptpa-web28-z01> (raw)
In-Reply-To: <DC011612-232F-47F3-A8AE-126FB2EDA02A@telia.com>


---- 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




      reply	other threads:[~2011-04-13 11:51 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-13 10:38 Difference letrec & environment binding Hans Aberg
2011-04-13 11:51 ` dsmich [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20110413115158.C5DYL.47361.root@cdptpa-web28-z01 \
    --to=dsmich@roadrunner.com \
    --cc=guile-devel@gnu.org \
    --cc=haberg-1@telia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).