6 ms·
How to slice your code into continuations
- deleted 7y ago[deleted]
- notacoward 7y agoIs this satire? The original four-line snippet is pretty darn clear in terms of intent, run-time cost, and potential errors or side effects. Slicing it into continuations so you can overload assignment, or vice versa, is making one mistake to enable another. > thinking about sequences of assignments as chains of function applications can be illuminating. "I love how this painting expresses the artist's existential dread. Please date me." > you can use this approach to pack more into a single line of Python If you think that's a worthy goal, then instant no-hire.
- dreamcompiler 7y ago> thinking about sequences of assignments as chains of function applications can be illuminating I think it's mind-bogglingly fascinating because it shines a bright spotlight on the deep connection between imperative and functional computing. If you don't think that's interesting, you're probably not somebody I would want to work for.
- paulddraper 7y agoI remember reading something on building a functional state machine that wound up being the equivalent of imperative programming, registers, etc.
- dreamcompiler 7y agoYep, two sides of the same coin. If you can find the reference I'd like to read it.
- TeMPOraL 7y agoOne of the particular insights I gained reading SICP long time ago is that local variables are equivalent to function parameters. That is (using CL syntax): (let ((a 3) (b 2)) (some code using a b)) Is equivalent to: (funcall (lambda (a b) (some code using a b)) 3 2) This is the basis of translating imperative loops into tail recursions.
- dreamcompiler 7y agoYou can go even further by currying the second one and then you're writing a Haskell compiler ;-)
- tombert 7y ago> The original four-line snippet is pretty darn clear in terms of intent, run-time cost, and potential errors or side effects. I think this is sort of an inherent problem of simple examples. CPS is pretty awesome, but it's not really worth it until you get into "Cheney on the MTA" problems with recursion and/or parallelism, but to show that in a simple tutorial would probably confuse a lot of people.
- jfoutz 7y agoI think it’s more of a lark to play with some ideas. I’m sure Dan can take his Oscar and get hired somewhere.
- danharaj 7y agoSeeing the classic middlebrow dismissal shitting on Dan Piponi is a special kind of entertainment.
- svat 7y agoThree Oscars actually! Funnily enough, even the very nice 2008 post linked at the bottom On writing Python one-liners (http://blog.sigfpe.com/2008/09/on-writing-python-one-liners.html http://blog.sigfpe.com/2008/09/on-writing-python-one-liners....), which opens with “You can think of what follows as the demonstration that you really don't want to” and ends with “But you probably don't want to do that”, has comments like “Have fun debugging! I'll stick with "normal" code thank you.”
- tel 7y agoMany people seem to write DSLs these days. My goal here was simply to encourage people who might not typically work in a functional style to think about how continuations might be useful in the design of their languages, even if it seems their language is imperative. And since you quote a line a few after that paragraph, it's not like you didn't give yourself the opportunity to read it. He's not trying to do very simple math. He's teaching fundamental DSL design techniques.
- neilv 7y agoContinuations are a powerful thing to know, but, for promoting DSLs, I hope no one thinks they have to wrangle continuations to implement DSLs. One way, if you're using Racket (or another Scheme or descendant), is to translate your DSL to normal syntax in Racket much like you'd write manually (and you can use imperative or only pure-functional features, your choice). For example, were I implementing the language used by the first example in the article, I might say each assignment establishes a lexical scope, and simply translate the source syntax to nested `let` form syntax: (let ((a (/ 1 (+ 1 7)))) (let ((b (* 2 (+ a (sqrt a))))) (let ((c (/ (atan b) (+ 1 c)))) (+ c (* 2 d))))) Or (since there wasn't any non-assignment code in between the assignments), how you might normally write it manually in Racket: (let* ((a (/ 1 (+ 1 7))) (b (* 2 (+ a (sqrt a)))) (c (/ (atan b) (+ 1 c)))) (+ c (* 2 d))) To get to that target syntax, I might have my parser produce intermediate syntax as a kind of abstract syntax tree, such as: (block (assignment a (/ 1 (+ 1 7))) (assignment b (* 2 (+ a (sqrt a)))) (assignment c (/ (atan b) (+ 1 c))) (return (+ c (* 2 d)))) Which, in this case, is a simple translation of syntax objects to the nested `let`s or `let*` examples above. Of course, for a real-world DSL, you might have very different semantics than in the simple example, but the above translation method still likely applies. You might start your language implementation by typing examples in your language, then the equivalents in Racket, then the intermediate parse trees of your examples... and then Racket `syntax-parse` or `syntax-case` generalizations your parse tree syntax to the Racket syntax. The examples you made might then be test cases for your translation. And if your DSL is embeddable in Racket code, you probably don't need the parser at all -- you might just need a `define-syntax`, with `syntax-parse`, `syntax-case`, or arbitrary code to do the translation.
- SatvikBeri 7y agoNot satire. The article focuses on how, rather than why, and uses the standard technique of starting with simple examples to illustrate the concept. If all lists had two elements in them, for loops wouldn't be particularly helpful. Similarly, continuations don't make the simple example mentioned any better, but they're useful in more complex situations. It does mention two non-trivial applications at the end: implementing list comprehensions in a language that doesn't have them (which are pretty useful!), and probabilistic programming.
- deleted 7y ago[deleted]
- mpalmer 7y ago> Is this satire? > "I love how this painting expresses the artist's existential dread. Please date me." > ...instant no-hire. Instant no-interview.
- bschwindHN 7y ago> then instant no-hire Classic HN brag: _I_ have the ability to not hire you!
- ofirg 7y agoTechnically, everyone has that ability.
- barrkel 7y agoMan, this place is way too fucking full of trolls.
- deleted 7y ago[deleted]
- brianberns 7y agoThe code snippet doesn't make sense to me: a = 1/(1+7) b = 2*a+sqrt(a) c = atan(b)/(1+c) return c+2*d Note that the assignment to 'c' depends on it already having a value?
- latchkey 7y ago... *d ?
- FridgeSeal 7y agoI took this to be a chunk of some wider bit of code in which c and d were defined previously and we’re interested in this section in particular.
- kwccoin 7y agoI suspect the 2nd "c" is "b" if you look a bit further the continuation. But as for the first "d" in return, no idea.
- bigred100 7y agoUse Newton’s method
- deleted 7y ago[deleted]