Hacker News new | past | comments | ask | show | jobs | submit login

I find an sample of Smalltalk is one of the most interesting ways to see how it flows as a language, as its grammar almost reads like English. From Wikipedia [1], this is a piece of Smalltalk code:

> 3 factorial + 4 factorial > between: 10 and: 100

which means:

> 3 receives the message "factorial" and answers 6 > 4 receives the message "factorial" and answers 24 > 6 receives the message "+" with 24 as the argument and answers 30 > 30 receives the message "between:and:" with 10 and 100 as arguments and answers true

[1]: https://en.wikipedia.org/wiki/Smalltalk




The thing I love about smalltalk is that the language grammar has about 7 rules in it and everything else is defined as passing messages to objects.

Want to add a method? You send the class a message and pass it a block with the method contents. Want define a class? You send the parent class the subclass message. Want an if statement? You evaluate a Boolean in a block and send it the ifTrue or ifFalse messages.

It means that sometimes it reads a bit strange, but there's a purity to it I love. Plus if you don't like it you can just define your own methods - for example I once did an (unfinished) library for GNU Smalltalk that gave it a more traditional if statement and loop constructs.


> as its grammar almost reads like English.

Really? How different our interpretations can be, that is incredibly opaque too me. Even reading your explanation I do not understand it.

Horses for courses, I guess


glacials made a typo that makes their explanation harder to folow. The correct smalltalk is:

    3 factorial + 4 factorial between: 10 and: 100
And the point about it reading like English is that because the syntax relies heavily on their fancy precedence rules you can make complex expressions without needing parentheses I think. It looks like the compiler magically knows what is meant by the expression, but the reality is that you need to be very aware of the grammar to be able to write it. It does look pretty in my opinion, but definitely opaque to someone not aware of the rules.

    Is 3 factorial plus 4 factorial between 10 and 100?


The precedence rules are: unary > binary > keyword. That's it. Various criticisms have been levelled at Smalltalk, but it's a very long stretch to claim that it is difficult for humans to learn.


Good thing that no one was claiming it is difficult to learn. Those precedence rules are very fancy indeed.


Yes. Clear as mud


Throwing in the parenthesis:

  ((3 factorial) + (4 factorial)) between: 10 and: 100.
Otherwise it's just left to right processing:

  3 + 4 * 10.
Becomes 70 as a consequence of these rules, the parser is pretty simple.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: