From 11d5afbe77c3ee661d26c8d42add54e951455b9b Mon Sep 17 00:00:00 2001
From: Shu Uesugi
- First, take a look at the following code. It’s an{' '}
+ Take a look at the following code. It’s an{' '}
If you apply the above function on a string{' '}
-
-
- …is represented by
- …is represented by
The above emoji puzzle is equivalent to the earlier JS code.
@@ -738,39 +738,50 @@ sushi => sushi`}
+
+
I’ve added the
- Now, we can also “run” the equivalent emoji puzzle and get the
- same result.{' '}
+ We can also “run” the equivalent emoji puzzle and get the same
+ result.{' '}
The result is a
Just like the JS code, the emoji puzzle ended up with a{' '}
Just like the JS code, the emoji puzzle ended up with a{' '} @@ -856,21 +856,32 @@ sushi => 'pizza'`} Visualizing evaluation rules
- Because you know how to code, you have a mental model of how
- function evaluation works. If you see{' '}
-
- On the other hand, non-programmers may not have a mental model of
- how function evaluation works. To help them develop a mental model
- without showing them any code, I created a{' '}
-
Let’s reuse the earlier example again:
- The above steps visually represent how functions are
- evaluated—they are equivalent to how JavaScript evaluates{' '}
-
+ By learning these rules, non-programmers will be able to evaluate + functions intuitively.
- That’s how the emoji puzzle visualizes the evaluation of a
- function when there’s no matching variable in the function
- body—like{' '}
-
- before pressing the
Let’s take a look at more complicated functional JS expressions - and see if they can be represented using an emoji puzzle. Check - out the following JS expression, and + and see if they can be represented using an emoji puzzle. +
+
+ Check out the following JS expression, and
The result was And here’s the equivalent emoji puzzle: Now, here’s the equivalent emoji puzzle: Let’s break it down. First, the function expression…
- Now, let’s talk about how we evaluate this function. In JS, we
- first evaluate the function call with the argument{' '}
+ How do we evaluate this function? Well, in JS, we first evaluate
+ the function call with the argument{' '}
Equivalently, in an emoji puzzle,{' '}
-
- If you look at the left edge of the puzzle, the bottom two rows
- correspond to the pair of{' '}
+ Also, if you look at the left edge of the puzzle above, the bottom
+ two rows correspond to the pair of{' '}
+ Let’s take a look at the next few steps in this iteration, which
+ is just like what we saw earlier:
Let’s take a look at the next few steps in this iteration:
We ended up with a
- Next, here’s slightly different JS code.{' '}
+ Next, here’s slightly different JS code. Compared to the last
+ time,{' '}
@@ -1307,11 +1326,11 @@ sushi => 'pizza'`}
the pair of
- before pressing the
+
+
equivalent to the above emoji puzzle
+
+ (but it’s not done yet!)
>
}
>{`(sandwich => 'hamburger')('chicken')`}
@@ -1224,14 +1242,13 @@ sushi => 'pizza'`}
So, it successfully calculated{' '}
-
+
+ If you’re thinking of taking a break, I’d appreciate it if you
+ could{' '}
+
+ From now on, we’ll apply what we’ve learned so far to solve more + interesting problems. +
- Let’s apply what we’ve learned and solve more interesting
- problems. First, here’s a function called{' '}
-
If we can express conditionals (e.g.
Here’s a function called
-
- These functions that can be converted to numbers using{' '}
-
Here’s an emoji puzzle that represents{' '} @@ -1538,22 +1547,21 @@ a => b => a(a(a(b))))
Other Church numeral functions can also be represented using emoji
- puzzles, and they can be converted as well. Of course, the emojis
- don’t need to be
Church numerals are interesting because{' '}
-
Let’s take a look.{' '} @@ -1629,18 +1629,31 @@ const zero = chicken => salad => salad convert(f(zero))`}
The result is
This is what I mean by “
-
First, here’s an emoji puzzle representation of the Church numeral{' '}
Let’s compute
@@ -1903,9 +1905,9 @@ const mul = sushi => sandwich => pizza => Conditionals
- In addition to math expressions, we can also represent{' '}
+ In addition to arithmetic, we can also implement{' '}
Consider the following JS code. This is a simple{' '} @@ -1920,8 +1922,12 @@ const mul = sushi => sandwich => pizza =>
It turns out that
As you just saw, in addition to math expressions, we can also
- represent
@@ -1993,8 +1998,8 @@ const mul = sushi => sandwich => pizza =>
- If we can express conditionals (e.g.
+ The answer is yes. We can express control flow using{' '}
Y combinator is complex, so if we go into detail we’ll need
another article. In fact, I used two full pages (
-
Take a look at this JS code. It calculates the{' '}
@@ -2136,15 +2143,16 @@ const mul = sushi => sandwich => pizza =>
- The above recursive function was a
+ Let me show you how. Here’s the Y combinator function{' '}
Now, we’ll apply
The result was
- In the future, I plan to do something similar with other CS
- topics. In the meantime, you can take a look at my course,{' '}
+ That’s how I came up with emoji puzzles (they’re
+ smartphone-friendly too). In the future, I plan to develop
+ something similar with other CS topics. In the meantime, you can
+ take a look at my course,{' '}
- For programmers interested in learning about Y Combinator, I recommend
- this video:{' '}
+ {onBlog ? <>If you are> : <>For programmers>} interested in learning
+ about Y Combinator, I recommend this video:{' '}