Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I've been chewing through a bit of The Little Schemer and the Peano examples got me thinking about operation size and time.

I got some help help on making Peano Multiplication linear -- however (time ...) didn't show much difference between a lexically scoped version and the let-loop version.

I do however find a massive difference between the lexically scoped and let-loop versions when minting a Peano Exponent function.

The obvious question is why it makes such a difference (or perhaps it does not and my attempt at a lexically scoped Peano Exponent has some fatal error)?

-- i had thought i understood what the let-loop was doing in terms of storing the value in the parent enclosure (making the two roughly equivalent); in which way is this wrong?

;; lexically scoped version of Peano Exp
(define (exxp-lex b ex)
 (define (exxp-aux ex prod)
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let b prod))
 (else
 (exxp-aux (- ex 1) (mX-lex b prod)))))
 (exxp-aux ex 1))
;; let version of Peano Exp
(define (exxp-let b ex)
 (let loop ((ex ex)
 (prod 1))
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let prod b))
 (else
 (loop (- ex 1) (mX-let prod b))))
 ))

If it's relevant, I am using petite-chez scheme.

For reference, here are the Peano Multiplication functions.

;; a lexically scoped version Peano Multiplication
(define (mX-lex n m)
 (define (mX-aux m product)
 (if (zero? m)
 product
 (mX-aux (- m 1) (+ product n))))
 (mX-aux m 0))
;; using let for Peano Mult
(define (mX-let n m)
 (let loop ((m m)
 (product 0))
 (if (zero? m)
 product
 (loop (- m 1) (+ product n)))))

I've been chewing through a bit of The Little Schemer and the Peano examples got me thinking about operation size and time.

I got some help on making Peano Multiplication linear -- however (time ...) didn't show much difference between a lexically scoped version and the let-loop version.

I do however find a massive difference between the lexically scoped and let-loop versions when minting a Peano Exponent function.

The obvious question is why it makes such a difference (or perhaps it does not and my attempt at a lexically scoped Peano Exponent has some fatal error)?

-- i had thought i understood what the let-loop was doing in terms of storing the value in the parent enclosure (making the two roughly equivalent); in which way is this wrong?

;; lexically scoped version of Peano Exp
(define (exxp-lex b ex)
 (define (exxp-aux ex prod)
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let b prod))
 (else
 (exxp-aux (- ex 1) (mX-lex b prod)))))
 (exxp-aux ex 1))
;; let version of Peano Exp
(define (exxp-let b ex)
 (let loop ((ex ex)
 (prod 1))
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let prod b))
 (else
 (loop (- ex 1) (mX-let prod b))))
 ))

If it's relevant, I am using petite-chez scheme.

For reference, here are the Peano Multiplication functions.

;; a lexically scoped version Peano Multiplication
(define (mX-lex n m)
 (define (mX-aux m product)
 (if (zero? m)
 product
 (mX-aux (- m 1) (+ product n))))
 (mX-aux m 0))
;; using let for Peano Mult
(define (mX-let n m)
 (let loop ((m m)
 (product 0))
 (if (zero? m)
 product
 (loop (- m 1) (+ product n)))))

I've been chewing through a bit of The Little Schemer and the Peano examples got me thinking about operation size and time.

I got some help on making Peano Multiplication linear -- however (time ...) didn't show much difference between a lexically scoped version and the let-loop version.

I do however find a massive difference between the lexically scoped and let-loop versions when minting a Peano Exponent function.

The obvious question is why it makes such a difference (or perhaps it does not and my attempt at a lexically scoped Peano Exponent has some fatal error)?

-- i had thought i understood what the let-loop was doing in terms of storing the value in the parent enclosure (making the two roughly equivalent); in which way is this wrong?

;; lexically scoped version of Peano Exp
(define (exxp-lex b ex)
 (define (exxp-aux ex prod)
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let b prod))
 (else
 (exxp-aux (- ex 1) (mX-lex b prod)))))
 (exxp-aux ex 1))
;; let version of Peano Exp
(define (exxp-let b ex)
 (let loop ((ex ex)
 (prod 1))
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let prod b))
 (else
 (loop (- ex 1) (mX-let prod b))))
 ))

If it's relevant, I am using petite-chez scheme.

For reference, here are the Peano Multiplication functions.

;; a lexically scoped version Peano Multiplication
(define (mX-lex n m)
 (define (mX-aux m product)
 (if (zero? m)
 product
 (mX-aux (- m 1) (+ product n))))
 (mX-aux m 0))
;; using let for Peano Mult
(define (mX-let n m)
 (let loop ((m m)
 (product 0))
 (if (zero? m)
 product
 (loop (- m 1) (+ product n)))))
edited tags
Link
200_success
  • 145.6k
  • 22
  • 190
  • 479
Source Link
ricardo
  • 265
  • 4
  • 10

lexical scope v let in scheme functions

I've been chewing through a bit of The Little Schemer and the Peano examples got me thinking about operation size and time.

I got some help on making Peano Multiplication linear -- however (time ...) didn't show much difference between a lexically scoped version and the let-loop version.

I do however find a massive difference between the lexically scoped and let-loop versions when minting a Peano Exponent function.

The obvious question is why it makes such a difference (or perhaps it does not and my attempt at a lexically scoped Peano Exponent has some fatal error)?

-- i had thought i understood what the let-loop was doing in terms of storing the value in the parent enclosure (making the two roughly equivalent); in which way is this wrong?

;; lexically scoped version of Peano Exp
(define (exxp-lex b ex)
 (define (exxp-aux ex prod)
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let b prod))
 (else
 (exxp-aux (- ex 1) (mX-lex b prod)))))
 (exxp-aux ex 1))
;; let version of Peano Exp
(define (exxp-let b ex)
 (let loop ((ex ex)
 (prod 1))
 (cond
 ((zero? ex) 1)
 ((= ex 1) (mX-let prod b))
 (else
 (loop (- ex 1) (mX-let prod b))))
 ))

If it's relevant, I am using petite-chez scheme.

For reference, here are the Peano Multiplication functions.

;; a lexically scoped version Peano Multiplication
(define (mX-lex n m)
 (define (mX-aux m product)
 (if (zero? m)
 product
 (mX-aux (- m 1) (+ product n))))
 (mX-aux m 0))
;; using let for Peano Mult
(define (mX-let n m)
 (let loop ((m m)
 (product 0))
 (if (zero? m)
 product
 (loop (- m 1) (+ product n)))))
lang-lisp

AltStyle によって変換されたページ (->オリジナル) /