Challenge 1
Write a program that increments the first character of a string.
C1 Solution
⊂+1⊢⟜(↘1)
or the idiomatic solution
⍜(⊢|+1)
Why?
Once you understand the idiomatic solution, there is no going back.
I solved this idiomatically but then I struggled with the intended solution.
I will admit that I just kinda forgot about on and made this horror
back join dip(add 1) dip first by drop 1
˜⊂ ⊙(+ 1) ⊙⊢ ⊸↘ 1
But let's look at what we're trying to accomplish, step by step.
Let's assume we want to turn bingo into cingo.
# What does on look like with drop 1?
on (drop 1) "bingo"
⟜(↘ 1) "bingo"
"ingo"
"bingo"
# And how is it different from by?
by drop 1 "bingo"
⊸↘ 1 "bingo"
"bingo"
"ingo"
# We want to operate on "bingo" first
# Therefore we choose `on`
first on (drop 1) "bingo"
⊢ ⟜(↘ 1) "bingo"
"ingo"
@b
# We want to change @b into @c
add 1 first on (drop 1) "bingo"
+ 1 ⊢ ⟜(↘ 1) "bingo"
"ingo"
@c
# Finally we join it
join add 1 first on (drop 1) "bingo"
⊂ + 1 ⊢ ⟜(↘ 1) "bingo"
"cingo"
But what about the idiomatic solution?
We have discussed under in chapter 2 but let's recap
What
underdoes is transform something, apply a function to the transformation and then undo the transformation.
What does under(first|add1) do to "bingo"?
- - transforms
bingointo@bviafirst- + You can think of this as dropping
ingo
- + You can think of this as dropping
- -
add 1to@bmaking@c - - reverses the transformation
- + "Undrops"
ingoback onto@cmakingcingo
- + "Undrops"