7 ms·
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.
by LiamPowell 3y ago
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)
? make_sum(make_product(multiplier(exp),
deriv(multiplicand(exp),
variable)),
make_product(deriv(multiplier(exp),
variable),
multiplicand(exp)))
: error(exp, "unknown expression type -- deriv");
}
Here is the original code:
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp)
(if (same-variable? exp var) 1 0))
((sum? exp)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
((product? exp)
(make-sum
(make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp))))
(else
(error "unknown expression type -- DERIV" exp))))
- Animats 3y agoUh 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.
- blagie 3y agoNo. If you think that's the whole point, you missed the point. Mathematical beauty was one of the ways to make the point, but far from the most important. As a footnote, one of the changes which happened since publication is the victory of functional. Note that both JavaScript and Python now embody many of the principles, design patterns, and best practices from SICP. As a second footnote, you're right: SCIP reads better than SICP. "Skippity do da..." (and if you want a third footnote, I know in the original it's zippity, but that's how kids in MY elementary school sang it, so to me, it will always be skippity"
- raincole 3y agoMaybe we have a different definition of "mathematical beauty", because I personally didn't find much mathematical beauty from SICP. I found the most helpful part of SICP is the chapter 4, but to call it mathematical is a bit like to say "everything is foundamentally math"...
- wmidwestranger 3y agoIt's not great and I'm not sure I'd play along but despite my own amazement, I've seen and been forced to do worse.
- dimaor 3y agoOne 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.
- andai 3y agoHaven't had the pleasure of dealing with this, but I'm told PHP has a uniquely broken idea of ternary: http://phpsadness.com/sad/30 http://phpsadness.com/sad/30
- jrajav 3y agoUnfortunately 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, nested ternaries is right out. In fact, usually in those other cases too, it's just annoying that there's not a good alternative.
- retrocryptid 3y agoWhich 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."
- paddw 3y agoa function _if(cond, a, b) would probably have been better.
- danShumway 3y agoBeat me to it :) It's not the biggest thing in the world, and I don't want to distract from the rest of the book, but this is a situation where writing a one or two line helper function: const _ = (cond, a, b) => cond ? a : b; would have made the code much more readable without much downside that I can see -- at least to my subjective opinion. Maybe I'm missing something. Edit: comment below correctly points out that if it's important for you to avoid immediate evaluation, you'll need to wrap your conditionals in functions.
- thr0waway42069 3y ago[flagged]
- deleted 3y ago[deleted]
- akavi 3y agoThe tragedy of JS if blocks being statements not expressions is made explicit here
- code_biologist 3y agoThere was a library adding macros to JS that was a thing long ago. I always thought that `cond` would make JSX way better, though JS + macros sounds terrifying in general. Code example below, `cond` is an example in their tutorial: https://www.sweetjs.org/doc/tutorial.html#sweet-cond https://www.sweetjs.org/doc/tutorial.html#sweet-cond let realTypeof = cond { case x === null: 'null' case Array.isArray(x): 'array' case typeof x === 'object': 'object' default: typeof x } Discussed in 2017 on HN: https://news.ycombinator.com/item?id=14695495 https://news.ycombinator.com/item?id=14695495
- 3cats-in-a-coat 3y agoWhat's wrong with ternary?
- vorticalbox 3y agoNothing but when you nest them a whole bunch they are far harder to read and understand that if/else.
- pipeline_peak 3y ago(is(((it any) harder than) this?))
- freilanzer 3y agoYes, since Lisp isn't hard to read.
- leodag 3y agoYou'd be surprised at how readable Lisp is for people who actually learn it instead of just saying "look, weird language ha-ha!"