Skip to content

Instantly share code, notes, and snippets.

@crclark96
Created April 6, 2020 18:27
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 crclark96/1d02f16d32f89c30c1b2ed003f5af3b3 to your computer and use it in GitHub Desktop.
Save crclark96/1d02f16d32f89c30c1b2ed003f5af3b3 to your computer and use it in GitHub Desktop.
pop :: [a] -> [a]
pop [] = []
pop (x:xs) = xs
compareWithBackspace :: String -> String -> Bool
compareWithBackspace xs ys = compare' s1 s2
where s1 = buildStack xs
s2 = buildStack ys
buildStack :: String -> String
buildStack xs = buildStack' xs []
buildStack' :: String -> String -> String
buildStack' [] ys = ys
buildStack' ('#':xs) ys = buildStack' xs $ pop ys
buildStack' (x:xs) ys = buildStack' xs (x:ys)
compare' :: Eq a => [a] -> [a] -> Bool
compare' xs ys = and $ (length xs == length ys) : (zipWith (==) xs ys)
GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help
Prelude> :l compareWithBackspace.hs
[1 of 1] Compiling Main ( compareWithBackspace.hs, interpreted )
Ok, modules loaded: Main.
*Main> compareWithBackspace "a##c" "#a#c"
True
*Main> compareWithBackspace "xy##" "z#w#"
True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment