SyntaxHighlighter

Thursday, August 25, 2011

print text-graph in haskell!

Recently, I've been fascinated by haskell. Here's my take on the same problem in the previous post solved in haskell.
print_chars_list :: [Int] -> Char -> IO ()
print_chars_list [] _ = putStr ""
print_chars_list (x:xs) c = do
    putStrLn (take x $ repeat c)
    print_chars_list xs c

-- example
print_chars_list [1,5,4,6,2] '."

Text-graph

/**
Problem: Given a list of positive Integers and a Char,
print the Char n times per line.
Eg. if I have [1,3,2,5] and '.' I want the output of:
.
...
..
.....
*/

print_chars = { l, c ->
  l.each { n ->
    for (int i = 0; i < n; i++) {
      print c
    }
    println ""
  }
}

// try using it.
print_chars([1,3,2,5], '.')