Created
January 10, 2024 15:09
-
-
Save ramirez7/ab2dd8a8b7e47c04069460257fe3535a to your computer and use it in GitHub Desktop.
Breaking down the answer to this question: https://www.reddit.com/r/haskell/comments/192d97p/just_a_simple_task_question/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
concat(zipWith replicate [1,2,1,0] "cold") | |
-- Desugar string literal to [Char] | |
concat(zipWith replicate [1,2,1,0] ['c', 'o', 'l', 'd']) | |
-- https://hackage.haskell.org/package/base-4.19.0.0/docs/src/GHC.List.html#zipWith | |
-- Rewritten to not use the "go" helper, zipWith is: | |
-- zipWith f [] _ = [] | |
-- zipWith f _ [] = [] | |
-- zipWith f (x:xs) (y:ys) = f x y : zipWith f xs ys | |
-- Substitute zipWith: | |
concat(replicate 1 'c' : zipWith replicate [2,1,0] ['o', 'l', 'd']) | |
-- Substitute replicate: | |
concat(['c'] : zipWith replicate [2,1,0] ['o', 'l', 'd']) | |
-- Keep going.. | |
-- Substitute zipWith: | |
concat(['c'] : replicate 2 'o' : zipWith replicate [1,0], ['l', 'd']) | |
-- Substitute replicate: | |
concat(['c'] : ['o', 'o'] : zipWith replicate [1,0], ['l', 'd']) | |
-- Substitute zipWith: | |
concat(['c'] : ['o', 'o'] : replicate 1 'l' : zipWith replicate [0], ['d']) | |
-- Substitute replicate: | |
concat(['c'] : ['o', 'o'] : ['l'] : zipWith replicate [0], ['d']) | |
-- Substitute zipWith: | |
concat(['c'] : ['o', 'o'] : ['l'] : replicate 0 'd' : zipWith replicate [] []) | |
-- Substitute replicate: | |
concat(['c'] : ['o', 'o'] : ['l'] : [] : zipWith replicate [] []) | |
-- Substitute zipWith (we've hit a base case): | |
concat(['c'] : ['o', 'o'] : ['l'] : [] : []) | |
-- Substitute concat: | |
['c', 'o', 'o', 'l'] | |
-- Re-sugar into string literal: | |
"cool" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment