Eddie Welker.com

Advanced web development, with a little bit about me thrown in too.

  • Blog The blog
  • About About me

February 10, 2008
Posted by Eddie

Writing Scheme in Javascript I

This is an interesting little function that I ran across in Kent Dybvig’s The Scheme Programming Language. I thought I would give it a go in javascript. I wrote it out, and ran into two problems. First, I wasn’t returning anything from the anonymous-self-executing function, so it was being garbage collected, and the call to tell() would give an undefined (secret didn’t exist anymore). The second was that I initially declared secret without the var which gave it global scope. Took me a little while to figure these out, but since I haven’t looked at any javascript in months, I don’t feel so bad.


/* the original function from The Scheme Programming Language
(define shhh #f)
(define tell #f)

(let ((secret 0))
  (set! shhh
    (lambda (message)
      (set! secret message)))
  (set! tell
    (lambda ()
      secret)))

(shhh "sally likes harry")
(tell)  "sally likes harry"
secret  Error: variable secret is not bound
*/
//the Javascript version of the same function
var shhh = false;
var tell = (function(){
 var secret = 0;
 shhh = function(message) { secret = message; }
 return function() { console.info(secret); }
})();

shhh("harry likes sally");
console.info("tell: " + tell() );

No Comments

Posted Under javascript

No Comments Yet

You can be the first to comment!

Leave a comment

You must be logged in to post a comment.

  • Me, elsewhere
    • tweets @ twitter
    • photos @ flickr
    • music @ last.fm
    • bookmarks @ delicious
    • articles @ google reader
    • events @ upcoming
    • location @ brightkite
    • updates @ friendfeed
    • books @ goodreads
    • me @ djangopeople
    • me @ facebook
  • My photos

    SidewalkTree and BrickAlissa and a CarebearDavid and AlissaEllenChair detail
  • What I'm listening to
    • Ny Philharmonic, Gil Shaham – NY Phil - Mother Goose, Barber Violin, Wooden Prince
    • Ny Philharmonic, Gil Shaham – NY Phil - Mother Goose, Barber Violin, Wooden Prince
    • Siouxsie and the Banshees – The Sweetest Chill
    • Siouxsie and the Banshees – Candyman
    • Siouxsie and the Banshees – Switch
    • Siouxsie and the Banshees – Suburban Relapse
    • Siouxsie and the Banshees – Nicotine Stain
    • Siouxsie and the Banshees – Metal Postcard
    • Siouxsie and the Banshees – Mirage
    • Siouxsie and the Banshees – Helter Skelter

This site is using the Handgloves WordPress Theme
Designed & Developed by George Wiscombe

Subscribe via RSS