You've hit your first trouble with Monads. What you probably want here is a let
statement inside your do
block
test n x = do
let cell1 = round (n * n * x)
cell2 = n * n - cell1
print cell1
print cell2
The difference here is that you can't assign directly inside of a do
block, since all do
blocks desugar to calls to >>=
and >>
. The let
statement allows you to define a local value like you can inside a function definition like
f x =
let y = 2 * x
z = y * y * y
in z + z + y
The way your function would desugar would be like
test n x =
let cell1 = round (n * n * x)
cell2 = n * n - cell1
in (print cell1 >> print cell2)
Where >>
just chains two monadic actions together. Note that this is not really how it desugars, I chose a representation that is equivalent in this case but it is not exactly what the compiler would actually generate.
let cell1 = ...
instead ofcell1 = ...
.