Live data from Hacker News

SICP in JavaScript

sourceacademy.org

51–60 of 126 posts

Re: SICP in JavaScript

#51

Earlier quoted context omitted.

What's wrong with ternary?

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(exp)
            ? make_sum(
                deriv(addend(exp), variable), 
                deriv(augend(exp), variable)
            )
            : is_product(exp)
            ? make_sum(
                make_product(multiplier(exp), deriv(multiplicand(exp), variable)), 
                make_product(deriv(multiplier(exp), variable), multiplicand(exp))
            )
            : error(exp, "unknown expression type -- deriv");
    }

Re: SICP in JavaScript

#52
post #16

Earlier quoted context omitted.

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

Or use a switch statement?

There's something satisfying about converting if/else tables to switch statements. I find them so much more readable.

Re: SICP in JavaScript

#53
The early sad parts of "SICP in JavaScript" are especially section 2.2.1 Representing Sequences and chapter 2.3 Symbolic Data:

"All the compound data objects we have used so far were constructed ultimately from numbers. In this section we extend the representational capability of our language by introducing the ability to work with strings of characters as data."

In SICP there were Lisp's symbolic expressions (s-expressions). They were removed without even mentioning them...

Re: SICP in JavaScript

#54

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…

The 0 and 1 are completely unnecessary though in some of these.

Example

is_same_variable(exp, variable) ? 1 : 0

If is_same_variable returns a truthy value then return 1 else 0.

We can remove that and it will still follow the same while also being more readable

Re: SICP in JavaScript

#55
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?

I think the Scheme version of SICP will live on outside of academia and CS undergrad studies. I'm using SICP for self-learning and I had the choice of Scheme, JS, or Python. I still chose Scheme. This is because I figured Scheme would offer something those other languages didn't since Scheme is the original language of the book and Scheme is just "different" than other languages. It forces you to think differently ab…

I actually used SICP when teaching a junior-level course on programming language principles and paradigms at San Jose State University, where the introductory language is Java. My students programmed largely in Scheme and Prolog, and they also got exposure to Haskell, Smalltalk, and Common Lisp.

I still believe there is room for SICP in the CS curriculum. While as an introductory language SICP and Scheme may lose out to more commercially popular languages like Python and Java, I still believe that eventually students should be exposed to Scheme as an introduction to functional programming and to demonstrate a minimal, highly flexible language as a vehicle for teaching programming language design and implementation. Then eventually the students can be introduced to the world of statically-typed functional programming languages like the ML family and Haskell.

Re: SICP in JavaScript

#56
I don’t know if anyone has bothered to point this out but you could keep your schemey SICP and go clojurescript if you wanna run on JavaScript but then again you still have to contend with cycles and reverse polish

Re: SICP in JavaScript

#57

Earlier quoted context omitted.

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…

The 0 and 1 are completely unnecessary though in some of these. Example is_same_variable(exp, variable) ? 1 : 0 If is_same_variable returns a truthy value then return 1 else 0. We can remove that and it will still follow the same while also being more readable

I'm not trying to change the code, I'm only formatting it.

Re: SICP in JavaScript

#58
post #19

Earlier quoted context omitted.

The tragedy of JS if blocks being statements not expressions is made explicit here

They are, but you use ?-: instead of if-else

That still doesn't make all blocks expressions.

In f# everything is an expression and it simplifies the language a lot. The last value in a block is considered the value "return" value of the whole block, which has the knockon effect of getting rid of most returns, allows you to check an if/else to return the same type and such. This also works in conjunction with the type inference, so you get an error when you try to return a string in one branch and an int in another.

Re: SICP in JavaScript

#59
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.

I see this opinion voiced all the time, and I can never understand how people can struggle with something like nested ternaries. Surely a straightforward use (i.e. not a weird edge case) of very basic syntax shared by most widely used programming languages shouldn't cause much of an issue? It should also be easier to reason about nested ternaries than an equivalent set of nested if-elses, because at least with ternar…

Should ternaries be left or right associative?

Re: SICP in JavaScript

#60
post #41

At no point in my entire existence would I subject myself to JavaScript voluntarily ever again. I would write my own damn scheme environment and use that instead of this.

Typescript is very nice. Too bad the JavaScript underneath is so… unprofessional.
Post reply on HN