|
14 | 14 | ;; People often send me emails asking, "How can I use lambda calculus to impress people?" |
15 | 15 | ;; Today, we find out. |
16 | 16 |
|
17 | | -;;  |
| 17 | +;;  |
18 | 18 |
|
19 | 19 | ;; I have an interview with ZCorp lined up in 5 minutes, |
20 | 20 | ;; and our challenge is to only use anonymous functions. |
|
52 | 52 | (conj (SELF SELF (rest LIST)) |
53 | 53 | (first LIST))))) |
54 | 54 | (REV REV [1 2 3 4 5]) |
55 | | -;;=> [5 4 3 2 1] |
56 | 55 |
|
57 | 56 | ;; `SELF` is an input to itself, the logic of reversal. |
58 | 57 |
|
|
70 | 69 | [] |
71 | 70 | (conj (SELF (rest LIST)) |
72 | 71 | (first LIST)))))) |
73 | | -;; ``` |
| 72 | +;; ```clojure |
74 | 73 | ;; ((REV' REV') [1 2 3 4 5]) |
75 | 74 | ;; ``` |
76 | 75 | ;; **error** |
|
90 | 89 | (first LIST)))))) |
91 | 90 |
|
92 | 91 | ((REV'' REV'') [1 2 3 4 5]) |
93 | | -;;=> [5 4 3 2 1] |
94 | 92 |
|
95 | 93 | ;; > That's a confusing way to write it |
96 | 94 |
|
|
116 | 114 | ;; But identity is the identity of itself: |
117 | 115 |
|
118 | 116 | (identity 1) |
119 | | -;;=> 1 |
120 | 117 |
|
121 | 118 | ((identity identity) 1) |
122 | | -;;=> 1 |
123 | 119 |
|
124 | 120 | ;; > O.K. sure, but that's a special case. |
125 | 121 |
|
126 | 122 | (((identity identity) (identity identity)) 1) |
127 | | -;;=> 1 |
128 | 123 |
|
129 | 124 | ;; > This is an identity crisis. |
130 | 125 |
|
|
187 | 182 |
|
188 | 183 | ;; Because `(FIX F) = ((FIX F) (FIX F))`, it was your idea to refactor remember? |
189 | 184 |
|
190 | | -;; ``` |
| 185 | +;; ```clojure |
191 | 186 | ;; (FIX REV-LOGIC) |
192 | 187 | ;; ``` |
193 | 188 | ;; **stack overflow** |
|
246 | 241 | ;; > At least it didn't blow up this time... |
247 | 242 |
|
248 | 243 | ((FIX REV-LOGIC) [1 2 3 4 5]) |
249 | | -;;=> [5 4 3 2 1] |
250 | 244 |
|
251 | 245 | ;; > Nice, that's the right answer. |
252 | 246 |
|
253 | 247 | ;; Even nicer is that our fixed logic behaves like identity now: |
254 | 248 |
|
255 | 249 | ((REV-LOGIC (FIX REV-LOGIC)) [1 2 3 4 5]) |
256 | | -;;=> [5 4 3 2 1] |
257 | 250 |
|
258 | 251 | ((REV-LOGIC (REV-LOGIC (FIX REV-LOGIC))) [1 2 3 4 5]) |
259 | | -;;=> [5 4 3 2 1] |
260 | 252 |
|
261 | 253 | ;; > I can't believe something so ridiculous actually works. |
262 | 254 |
|
|
274 | 266 | ;; Your essence is invariant. |
275 | 267 |
|
276 | 268 | ((Z REV-LOGIC) [1 2 3 4 5]) |
277 | | -;;=> [5 4 3 2 1] |
278 | 269 |
|
279 | 270 | ;; > Wait, we are meant to be doing Fibonacci, remember? |
280 | 271 |
|
|
283 | 274 | ;; > It looks to me like you doubled the code, that's not great refactoring. |
284 | 275 | ;; > Using single letters make it totally unreadable. |
285 | 276 |
|
286 | | -;; Hmmm you got me there, there does seem to be a lot of doubling. |
287 | | -;; What if we had a function for `f => (f f)` |
| 277 | +;; Hmmm, there does seem to be a lot of doubling. |
| 278 | +;; We can factor out a function for `f => (f f)`. |
288 | 279 |
|
289 | 280 | (def REPLICATE "Omega, the self-devouring serpent" |
290 | 281 | (fn [F] |
|
293 | 284 | ;; The replication of identity is itself. |
294 | 285 |
|
295 | 286 | ((REPLICATE identity) 1) |
296 | | -;;=> 1 |
297 | 287 |
|
298 | 288 | ;; But test not the serpent lightly |
299 | 289 |
|
300 | | -;; ``` |
| 290 | +;; ```clojure |
301 | 291 | ;; (REPLICATE REPLICATE) |
302 | 292 | ;; ``` |
303 | 293 | ;; **stack overflow** |
|
311 | 301 | (LOGIC (fn [V] ((X X) V))))))) |
312 | 302 |
|
313 | 303 | ((Z REV-LOGIC) [1 2 3 4 5]) |
314 | | -;;=> [5 4 3 2 1] |
315 | 304 |
|
316 | 305 | ;; > That's not really any clearer... |
317 | 306 |
|
|
332 | 321 | ;; OMEGA diverges, ZETA folds, LOGIC writes QED. |
333 | 322 |
|
334 | 323 | ((FOLD REV-LOGIC) [1 2 3 4 5]) |
335 | | -;;=> [5 4 3 2 1] |
336 | 324 |
|
337 | 325 | ;; That's much nicer, I'm so glad you suggested using longer names. |
338 | 326 |
|
|
348 | 336 | (SELF (concat [(+ A B) B] FIBS)))))) |
349 | 337 |
|
350 | 338 | ((FOLD FIB-LOGIC) [1 1]) |
351 | | -;;=> (13 8 8 5 5 3 3 2 2 1 1 1) |
352 | 339 |
|
353 | 340 | ;; > That's all backward!! |
354 | 341 |
|
355 | 342 | ;; Oh, my mistake |
356 | 343 |
|
357 | 344 | ((FOLD REV-LOGIC) ((FOLD FIB-LOGIC) [1 1])) |
358 | | -;;=> [1 1 1 2 2 3 3 5 5 8 8 13] |
359 | 345 |
|
360 | 346 | ;; > You can't be serious... |
361 | 347 | ;; > This is ridiculous. |
|
372 | 358 | (cons A ((SELF B) (+ A B)))))))) |
373 | 359 |
|
374 | 360 | (take 20 (((FOLD FIB-LOGIC-FOREVER) 1) 1)) |
375 | | -;;=> (1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765) |
376 | 361 |
|
377 | 362 | ;; That's so nice. |
378 | 363 |
|
379 | 364 | ;; > Oh look at the time! I have a more important meeting to go to! |
380 | 365 | ;; **disconnects** |
381 | | -;; |
| 366 | + |
382 | 367 | ;; Ouch, Rough. |
383 | | -;; ZCorp never got back to me, so let’s update the scoreboard as a loss without a GG. |
| 368 | +;; ZCorp never got back to me, so let’s update the scoreboard as a loss. |
384 | 369 |
|
385 | 370 | ^:kind/table ^:kindly/hide-code |
386 | 371 | {:Interviews [1] |
|
0 commit comments