Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variable from do block assignment line in a where clause?

I have the following sample of code:

{-# LANGUAGE ScopedTypeVariables #-}

main = do
  putStrLn "Please input a number a: "
  a :: Int  <- readLn
  print a

  putStrLn "Please input a number b: "
  b :: Int  <- readLn
  print b

  putStrLn ("a+b+b^2:" ++ (show $ a+b+c))
    where c = b^2

For some reason I cannot use variable b in a where clause, the error I get is the following:

Main3.hs:13:15: error: Variable not in scope: b
   |
13 |     where c = b^2
   |               ^

Any ideas how to make b available in the where clause?

like image 317
altern Avatar asked Jan 12 '20 22:01

altern


1 Answers

Use let instead of where:

{-# LANGUAGE ScopedTypeVariables #-}

main = do
  putStrLn "Please input a number a: "
  a :: Int  <- readLn
  print a

  putStrLn "Please input a number b: "
  b :: Int  <- readLn
  print b

  let c = b^2
  putStrLn ("a+b+b^2:" ++ (show $ a+b+c))

The reason for the problem is that variables in the where clause are in scope for all of main, but b isn't in scope until after b :: Int <- readLn. In general, where clauses can't reference variables bound inside of a do block (or anywhere to the right of the =, for that matter: e.g., f x = y*2 where y = x+1 is fine but f = \x -> y*2 where y = x+1 is not).

like image 119
Joseph Sible-Reinstate Monica Avatar answered Nov 15 '22 13:11

Joseph Sible-Reinstate Monica