0
votes

I wanted to Print to A pascal Triangle for a given Length.

main = do 
  l_str <- getLine
  let l_int = read $ l_str :: Int     
  let why = print_row l_int 0
  print why
  return ()

print_row x y
     | (x < y) = " "    
     | otherwise = (print_column y 0 ) ++ "\n" ++ print_row x (y+1) 
print_column y r
     | (y < r) = ""
     | otherwise = (show $ fact y r ) ++ print_column y (r+1)   
fact n r
     |  (n >= r) = truncate $ (fact' n)/((fact' (n-r))*(fact' r))
fact' n
     | (n >= 0) = product [1..n]    

I have checked all my functions "print_row" ,"print_column" everything works fine. I am getting this error:

PascalTriangle.hs:4:17:
No instance for (RealFrac Int) arising from a use of ‘fact’
In the expression: fact l_int 0
In an equation for ‘why’: why = fact l_int 0
In the expression:
do { l_str <- getLine;
let l_int = ...;
let why = fact l_int 0;
print why;
.... }

I am not able Understand anything about this error.The pogram works fine when I use a constant instead of l_int in line 4.Like let why = print_row 4 0.

1
A general strategy that often helps with errors like this is to add type signatures to your functions. - user2297560
Unrelated to your problem, but you may prefer readLn over read <$> getLine in the future; on parse errors, the former throws an IO exception right away, instead of a difficult-to-trace pure exception at some ill-defined later point. - Daniel Wagner

1 Answers

0
votes

You need to use div instead of /.

div will take two Integral values and return another Integral value - e.g. div 5 2 == 2. Then you'll also need to get rid of the truncate call.

/ does "floating point" division.

fromIntegral will convert an Integral value to any other Num type.