Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Created January 10, 2024 15:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramirez7/ab2dd8a8b7e47c04069460257fe3535a to your computer and use it in GitHub Desktop.
Save ramirez7/ab2dd8a8b7e47c04069460257fe3535a to your computer and use it in GitHub Desktop.
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