9 "from notebook_preamble import J, V, define"
13 "cell_type": "markdown",
16 "# [Quadratic formula](https://en.wikipedia.org/wiki/Quadratic_formula)"
20 "cell_type": "markdown",
23 "Cf. [jp-quadratic.html](http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html)"
27 "cell_type": "markdown",
30 " -b ± sqrt(b^2 - 4 * a * c)\n",
31 " --------------------------------\n",
36 "cell_type": "markdown",
39 "$\\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$"
43 "cell_type": "markdown",
46 "## Write a straightforward program with variable names.\n",
47 "This math translates to Joy code in a straightforward manner. We are going to use named variables to keep track of the arguments, then write a definition without them."
51 "cell_type": "markdown",
59 "cell_type": "markdown",
62 "### `sqrt(b^2 - 4 * a * c)`\n",
63 " b sqr 4 a c * * - sqrt"
67 "cell_type": "markdown",
75 "cell_type": "markdown",
79 "There is a function `pm` that accepts two values on the stack and replaces them with their sum and difference.\n",
81 " pm == [+] [-] cleave popdd"
85 "cell_type": "markdown",
88 "### Putting Them Together\n",
90 " b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2\n",
92 "We use `app2` to compute both roots by using a quoted program `[2a /]` built with `cons`."
96 "cell_type": "markdown",
99 "## Derive a definition.\n",
100 "Working backwards we use `dip` and `dipd` to extract the code from the variables:\n",
102 " b neg b sqr 4 a c * * - sqrt pm a 2 * [/] cons app2\n",
103 " b [neg] dupdip sqr 4 a c * * - sqrt pm a 2 * [/] cons app2\n",
104 " b a c [[neg] dupdip sqr 4] dipd * * - sqrt pm a 2 * [/] cons app2\n",
105 " b a c a [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
106 " b a c over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
108 "The three arguments are to the left, so we can \"chop off\" everything to the right and say it's the definition of the `quadratic` function:"
113 "execution_count": 2,
117 "define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2')"
121 "cell_type": "markdown",
129 "execution_count": 3,
134 "output_type": "stream",
136 "-0.3819660112501051 -2.618033988749895\n"
141 "J('3 1 1 quadratic')"
145 "cell_type": "markdown",
148 "If you look at the Joy evaluation trace you can see that the first few lines are the `dip` and `dipd` combinators building the main program by incorporating the values on the stack. Then that program runs and you get the results. This is pretty typical of Joy code."
153 "execution_count": 4,
158 "output_type": "stream",
160 " . -5 1 4 quadratic\n",
161 " -5 . 1 4 quadratic\n",
162 " -5 1 . 4 quadratic\n",
163 " -5 1 4 . quadratic\n",
164 " -5 1 4 . over [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
165 " -5 1 4 1 . [[[neg] dupdip sqr 4] dipd * * - sqrt pm] dip 2 * [/] cons app2\n",
166 "-5 1 4 1 [[[neg] dupdip sqr 4] dipd * * - sqrt pm] . dip 2 * [/] cons app2\n",
167 " -5 1 4 . [[neg] dupdip sqr 4] dipd * * - sqrt pm 1 2 * [/] cons app2\n",
168 " -5 1 4 [[neg] dupdip sqr 4] . dipd * * - sqrt pm 1 2 * [/] cons app2\n",
169 " -5 . [neg] dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
170 " -5 [neg] . dupdip sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
171 " -5 . neg -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
172 " 5 . -5 sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
173 " 5 -5 . sqr 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
174 " 5 -5 . dup mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
175 " 5 -5 -5 . mul 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
176 " 5 25 . 4 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
177 " 5 25 4 . 1 4 * * - sqrt pm 1 2 * [/] cons app2\n",
178 " 5 25 4 1 . 4 * * - sqrt pm 1 2 * [/] cons app2\n",
179 " 5 25 4 1 4 . * * - sqrt pm 1 2 * [/] cons app2\n",
180 " 5 25 4 4 . * - sqrt pm 1 2 * [/] cons app2\n",
181 " 5 25 16 . - sqrt pm 1 2 * [/] cons app2\n",
182 " 5 9 . sqrt pm 1 2 * [/] cons app2\n",
183 " 5 3.0 . pm 1 2 * [/] cons app2\n",
184 " 8.0 2.0 . 1 2 * [/] cons app2\n",
185 " 8.0 2.0 1 . 2 * [/] cons app2\n",
186 " 8.0 2.0 1 2 . * [/] cons app2\n",
187 " 8.0 2.0 2 . [/] cons app2\n",
188 " 8.0 2.0 2 [/] . cons app2\n",
189 " 8.0 2.0 [2 /] . app2\n",
190 " [8.0] [2 /] . infra first [2.0] [2 /] infra first\n",
191 " 8.0 . 2 / [] swaack first [2.0] [2 /] infra first\n",
192 " 8.0 2 . / [] swaack first [2.0] [2 /] infra first\n",
193 " 4.0 . [] swaack first [2.0] [2 /] infra first\n",
194 " 4.0 [] . swaack first [2.0] [2 /] infra first\n",
195 " [4.0] . first [2.0] [2 /] infra first\n",
196 " 4.0 . [2.0] [2 /] infra first\n",
197 " 4.0 [2.0] . [2 /] infra first\n",
198 " 4.0 [2.0] [2 /] . infra first\n",
199 " 2.0 . 2 / [4.0] swaack first\n",
200 " 2.0 2 . / [4.0] swaack first\n",
201 " 1.0 . [4.0] swaack first\n",
202 " 1.0 [4.0] . swaack first\n",
203 " 4.0 [1.0] . first\n",
209 "V('-5 1 4 quadratic')"
214 "execution_count": null,
222 "display_name": "Python 3 (ipykernel)",
223 "language": "python",
231 "file_extension": ".py",
232 "mimetype": "text/x-python",
234 "nbconvert_exporter": "python",
235 "pygments_lexer": "ipython3",