user1501127
user1501127

Reputation: 865

Formatting strings in a table

I'm still trying to solve an issue I have with string formatting and I just can't seem to get it working.

I have two functions that give me Integers out as they are just calculations and I want to create a string with these two calculations in a third and new output AND make it nice and pretty. I'd like to present it in like a table (colums and rows).

What I am trying so far is:

   tableShow :: Int -> Int -> String
   tableShow n 0 = putStrLn (n  power1  power2)
   tableShow n k 
       | let list_k = [0..k]
       | k > 0      =  show(unlines[n ++ "\t" calc1 x k ++ "\t" ++ "\t" ++ calc2 x k | x <- list_k])

calc1 and calc2 just take two Integers given by user and do a simple calculation on them, returning a value. At least I got those working like a charm. :)

Anyone have a good idea where I am going wrong?!? Is there someone who can point me in the right direction?

Any and all ideas and suggestions will be greatly appreciated, I've been at this all weekend now :/

//regards

Upvotes: 0

Views: 192

Answers (2)

NovaDenizen
NovaDenizen

Reputation: 5325

To turn basic values like Ints into Strings, use show.

multMsg x y = "The product of " ++ show x ++ " and " ++ show y ++ " is " ++ show (x*y)

I usually use concat in this situation though.

multMsg2 x y = concat ["The product of ", show x, " and ", show y, " is ", show (x*y)]

Upvotes: 2

Brandon Pickering
Brandon Pickering

Reputation: 315

tableShow :: Int -> Int -> String
tableShow n 0 = putStrLn (n  power1  power2)
tableShow n k 
    | let list_k = [0..k]
    | k > 0      =  show(unlines[n ++ "\t" calc1 x k ++ "\t" ++ "\t" ++ calc2 x k | x <- list_k])

You didn't mention whether your problem was an error or just that your function doesn't work as intended, but your let binding syntax is off, and you missed a ++. I don't know what you intended for n power1 power2, but I'll guess that you wanted multiplication. It should be:

tableShow :: Int -> Int -> String
tableShow n 0 = putStrLn (n * power1 * power2)
tableShow n k
    | k > 0 = let list_k = [0..k] in 
              show(unlines[n ++ "\t" ++ calc1 x k ++ "\t" ++ "\t" ++ calc2 x k | x <- list_k])

You may want to look over a tutorial or two to get the syntax down.

Upvotes: 4

Related Questions