Live data from Hacker News

SICP in JavaScript

sourceacademy.org

71–80 of 126 posts

Re: SICP in JavaScript

#71
post #64

Earlier quoted context omitted.

Should ternaries be left or right associative?

Right associative! It's just one of the many rites of passage for people working with PHP to get bitten by its left associative ternaries. Naked nested ternaries are deprecated now, but maybe one day, PHP can have right associative ternaries.

Except when it's not, like in perl.

Re: SICP in JavaScript

#72
post #16
post #12

Earlier quoted context omitted.

One thing I have learnt very well from rewriting a legacy PHP code written by amateur teams is: never ever nest ternary operations. maybe it's only me, but it's really hard to reason about this.

Unfortunately there is no if-expression in JS so sometimes it's awkward _not_ to use ternaries in multi-line statements - for instance, when writing in an expression only context like a string interpolation or JSX. It's also just annoying to not be able to assign conditionally without using it, instead of a more clear and readable if/else. It's one of the more annoying nits of the Algol legacy. Oh, but fully agreed,…

Which is one of the reasons why it's difficult to interpret javascript as suitably functional to apply SICP to it.

"Languages do not differ in what they make possible, but in what they make easy."

Re: SICP in JavaScript

#73

Earlier quoted context omitted.

Nothing but when you nest them a whole bunch they are far harder to read and understand that if/else.

I frankly find it hard to imagine an expression form of if...else that's more readable than either the JS or Scheme versions of the code. In fact, I believe the problem with the JS example's readability is the formatting. Here's how I'd format the same code, and I find this quite readable: function deriv(exp, variable) { return is_number(exp) ? 0 : is_variable(exp) ? is_same_variable(exp, variable) ? 1 : 0 : is_sum(e…

Just for looksies.

    function deriv(exp,v){
      //v = variable;
      if(is_number(exp) ){ return 0 }
      if(is_variable(exp){ return is_same_variable(exp,v) * 1 }
      if(is_sum(exp)){ 
         a = deriv(addend(exp),v);
         return make_sum(a,a)
      }
      if(is_product(exp)){
         m = multiplier(exp);
         c = multiplicand(exp); 
         a = make_product(m,deriv(c,v));
         b = make_product(deriv(m,v),c));
         return make_sum(a,b)
      }
      return error(exp, "unknown expression type -- deriv");
    }

Re: SICP in JavaScript

#74
post #69

Earlier quoted context omitted.

What's deterring me from pulling the trigger on the Scheme version is the price. CAD$87 seems a bit steep for a paperback edition that's almost 30 years old. The JavaScript version is CAD$20 cheaper.

Seeing a lot of $25-45 (USD) used price. Used books are awesome. I have north of 500 books and I doubt more than 50 were purchased new. Only that many because some of my relatives want Christmas and birthday wishlists every year, but won't shop anywhere but Amazon and think it's weird to buy someone a used book as a gift (I'd rather have 2-3x as many books for the same money, but hey, I'm not the one paying, so whate…

I ended up at Alibris. CAD$45 + CAD$15 shipping for 'very good' condition.

Re: SICP in JavaScript

#75
post #14

What I loved most about my class based on SICP was that I learned a whole new way to reason about code, and that came from using Scheme. They changed the intro class at Berkeley to use Python a while back, when the folks who knew lisp/scheme retired, and I think it made the class much worse. This is really the death knell of SICP isn't it?

You know how people love to repeat that old saw about how computer science is no more about computers than astronomy is about telescopes (or whatever it is)? Well ironically (because it's the same exact people but this time on the other side of the misunderstanding) SICP is no more about scheme/lisp than computer science is about computers.

SICP is not about scheme or lisp, but its concepts are best understood in scheme in lisp. Just like the best way to understand what Saturn looks like is to look at it though a telescope.

Sure you can use a spectrum scan or the output of a radio telescope, but it's really not the same as looking at in a telescope.

Same thing here -- SICP just isn't the same if it's not in scheme or lisp.

Re: SICP in JavaScript

#76
post #14

What I loved most about my class based on SICP was that I learned a whole new way to reason about code, and that came from using Scheme. They changed the intro class at Berkeley to use Python a while back, when the folks who knew lisp/scheme retired, and I think it made the class much worse. This is really the death knell of SICP isn't it?

You know how people love to repeat that old saw about how computer science is no more about computers than astronomy is about telescopes (or whatever it is)? Well ironically (because it's the same exact people but this time on the other side of the misunderstanding) SICP is no more about scheme/lisp than computer science is about computers.

I've tried to go through the JS version, and I have to disagree. I know enough Scheme to see why the book is structured the way it is, but it doesn't work well in JS. The patterns that seem so natural in Scheme are contrived to the point of being distracting in JavaScript.

Re: SICP in JavaScript

#77

It seems like they have attempted to write scheme using JavaScript syntax to avoid having to make any significant changes to the non-code sections of the book. Here is an excerpt, it's not good: function deriv(exp, variable) { return is_number(exp) ? 0 : is_variable(exp) ? is_same_variable(exp, variable) ? 1 : 0 : is_sum(exp) ? make_sum(deriv(addend(exp), variable), deriv(augend(exp), variable)) : is_product(exp) ? m…

Uh oh. That's like the original Numerical Recipes in C. All the arrays had their first value as 1, instead of 0. It was a conversion from FORTRAN, where arrays really did start at 1. The Numerical Recipes authors provided an array allocator which generated an address offset before the beginning of the array to make that work. It was awful. I rewrote the algorithms I needed with sane layout.

The whole point of SCIP was mathematical beauty. Losing that in translation is not good.

Re: SICP in JavaScript

#78
post #69

Earlier quoted context omitted.

What's deterring me from pulling the trigger on the Scheme version is the price. CAD$87 seems a bit steep for a paperback edition that's almost 30 years old. The JavaScript version is CAD$20 cheaper.

Seeing a lot of $25-45 (USD) used price. Used books are awesome. I have north of 500 books and I doubt more than 50 were purchased new. Only that many because some of my relatives want Christmas and birthday wishlists every year, but won't shop anywhere but Amazon and think it's weird to buy someone a used book as a gift (I'd rather have 2-3x as many books for the same money, but hey, I'm not the one paying, so whate…

SICP, as a book, is very nice. It's a great size, not too big, light and nimble to carry around. Many books, notably text books, are pretty big and ungainly. But SICP is just a really nice package. (Mind, I have not seen the paperback, I have a hardback from mutter mumble years ago.)

Re: SICP in JavaScript

#79

Earlier quoted context omitted.

If you want the equivalent of if-expression in JS, I think it is much better to make a separate function with a number of returns. In this case the function is already there: function deriv(exp, variable) { if (is_number(exp)) { return 0; } if (is_variable(exp)) { if (is_same_variable(exp, variable)) { return 1; } else { return 0; } } if (is_sum(exp)) { return make_sum(deriv(addend(exp), variable), deriv(augend(exp),…

It's subjective, but seconded (switch statements are also great for this because they make fall-through logic more obvious). I'll add a couple of things onto this: early returns are very helpful for me in avoiding nesting if statements (although that's less applicable in this specific example). function op(cond) { if (cond) { //do something } } function op (cond) { if (!cond) { return; } //do something } And it's goo…

Refreshing to see some love for early return. Often people like to say they are an anti pattern, but then you have to maintain each layer of if nesting in your head (as they are sometimes off screen) when reasoning about code in the middle instead of handling edge cases first and leaving the rest of the method for the core/common case.

Re: SICP in JavaScript

#80

Earlier quoted context omitted.

If you want the equivalent of if-expression in JS, I think it is much better to make a separate function with a number of returns. In this case the function is already there: function deriv(exp, variable) { if (is_number(exp)) { return 0; } if (is_variable(exp)) { if (is_same_variable(exp, variable)) { return 1; } else { return 0; } } if (is_sum(exp)) { return make_sum(deriv(addend(exp), variable), deriv(augend(exp),…

if (is_same_variable(exp, variable)) { return 1; } else { return 0; } should be return +is_same_variable(exp, variable)

Yeah, though might want to use Number(is_same_variable(..)) for additional clarity.
Post reply on HN