3 ms·
Hello, Hacker News! I'm new to the community. I just saw this and thought it'd be fun to solve in Haskell. The first solution I thought of was done in under a
by hyperrealgopher 6y ago
Hello, Hacker News! I'm new to the community. I just saw this and thought it'd be fun to solve in Haskell. The first solution I thought of was done in under a couple minutes. The second solution (I wanted to implement `group` myself) took a lot longer because I messed up a pattern match!
https://gist.github.com/hyperrealgopher/19730725804ba8825d50d96f75b88a96 https://gist.github.com/hyperrealgopher/19730725804ba8825d50...
- solresol 6y ago> module Main where > input = "aaaabbbcca" > r :: String -> (Char,Int) -> [(String, Int)] > r [] (c,n) = [([c],n)] > r (x:xs) (c,n) > | c == x = r xs (c,n+1) > | otherwise = (([c],n): r xs (x,1)) > main = putStrLn (show (r (tail input) (head input, 1) )) 4 minutes 42 seconds. And no, I don't write code like this normally, but I guess this is me when I'm under time pressure... anyway, I could refactor it nicely afterwards in the 25 minutes allocated.
- deleted 6y ago[deleted]
- hyperrealgopher 6y agoGreat solution that avoids using any functions that do the heavy lifting for you! Thanks for showing!
- zzo38computer 6y agoMy own first idea was actually similar to yours, although I wrote: map (head &&& length) . group (Also, it took me only a few seconds, because it just seemed obvious to me, even though Haskell is not the programming language I normally use for my projects, but it seems the right thing for this.)
- deleted 6y ago[deleted]
- hyperrealgopher 6y agoHa, what a coincidence! Right before you posted this I just remembered (&&&) exists: https://gist.github.com/hyperrealgopher/19730725804ba8825d50d96f75b88a96/revisions https://gist.github.com/hyperrealgopher/19730725804ba8825d50... Thanks for sharing!