3 ms·
Quadratic formula in joy: DEFINE quadratic-3 == # a b c => [root1 root2 ] [] cons cons cons
by dvh 8y ago
Quadratic formula in joy:
DEFINE
quadratic-3 == # a b c => [root1 root2 ]
[] cons cons cons # list of parameters
[ [ [ [dup * swap] dip * 4 * - sqrt ] # radical
[ pop 0 swap - ] # minusb
[ 2 * ] ] # divisor
[i] map ]
infra first
[ [ [ + swap / ] # root1
[ - swap / ] ] # root2
[i] map ]
infra first.
- geocar 8y agoThat doesn't seem very... joyful. In q it's just {(q%x),z%q:-.5y+signum[y]sqrt(yy)-4xz} This is easy to see is correct. How do you know that Joy is correct? I'm trying to mentally execute this. I know I haven't seen Joy before, but I've used Forth, Postscript and Lisp, so I feel like I have a lot to stand on: - cons seems to have the same meaning in lisp, so x y cons -> (x . y) or in Erlangish, [x|y] right? - x [y] dip seems to be y x - [x] [y] infra seems to be the same as [x y] - swap, first, pop, sqrt all seem reasonable - wouldn't [] cons cons cons dup* be better than the extra list with [i] map ] infra first? it's not really clear to me what [i] map is doing... I see how the values have to flow, but it feels like it should be redundant. I feel like it's immediately a lot more (mental) work. It reminds me of tracing forth code before I just gave in and started using variables like a normal person. Is this really the best way to be introduced to Joy? Is this a typical problem that Joy programmers feel is the right way to program (and the right solution?) Am I missing something here?
- exikyut 8y agoFormatting note: It will look a bit messier for the things*involving*asterisks to be on their own line (indented two spaces), but that way Arc won't chew them. I'm mentioning this because I can't determine the original text myself.
- geocar 8y agoYeah, can't edit now. {(q%x),z%q:-.5*y+signum[y]*sqrt(y*y)-4*x*z}
- mcguire 8y agoI haven't done much with concatenate languages, but a rule of thumb is that if you see a bunch of dips, dups, and swaps, then you are probably doing something wrong. Those should be encapsulated in simple definitions.