DEV Community

Discussion on: Daily Challenge #202 - Complete the Pattern II

Collapse
 
avalander profile image
Avalander • Edited

Haskell

I'm writing this on my phone, so there are probably errors, I'll check it when I have access to a compiler.

pattern :: Int -> String
pattern n
  | n < 1     = " "
  | otherwise = pattern' n
  where
    pattern' n = unlines $ foldl folder [] $ map show [n,(n-1)..1]
    folder :: [String] -> String -> [String]
    folder [] x     = [x]
    folder (p:xs) x = (p ++ x) : p : xs

Edit: fixed issues after running it through a compiler.

Collapse
 
craigmc08 profile image
Craig McIlwrath

I do a lot of these challgnes on my phone. I use rextester.com/l/haskell_online_com... to test my Haskell code a lot.

Collapse
 
avalander profile image
Avalander

Thanks for the tip!