Printing a field with additional dots in haskell

Posted by Frank Kluyt on Stack Overflow See other posts from Stack Overflow or by Frank Kluyt
Published on 2013-11-04T15:21:42Z Indexed on 2013/11/04 15:54 UTC
Read the original article Hit count: 243

I'm writing a function called printField. This function takes an int and a string as arguments and then then prints a field like this "Derp..." with this: printField 7 "Derp". When the field consists of digits the output should be "...3456".

The function I wrote looks like this:

printField :: Int -> String -> String
printField x y = if isDigit y 
                 then concat(replicate n ".") ++ y
                 else y ++ concat(replicate n ".")
                 where n = x - length y

This obviously isn't working. The error I get from GHC is:

Couldn't match type `[Char]' with `Char'
    Expected type: Char
      Actual type: String
    In the first argument of `isDigit', namely `y'
    In the expression: isDigit y
    In the expression:
      if isDigit y then
          concat (replicate n ".") ++ y
      else
          y ++ concat (replicate n ".")

I can't get it to work :(. Can anyone help me out? Please keep in mind that I'm new to Haskell and functional programming in general.

© Stack Overflow or respective owner

Related posts about haskell

Related posts about functional-programming