1

I have just started learning Haskell. I'm beginning by writing a simple function that takes two values n and x, and then displays two integer values computed with it.

test n x = do
    cell1 = round(n*n*x)
    cell2 = n*n - cell1
    print cell1
    print cell2

But it doesn't run and keeps giving me a Parse error on input `=' error. What is happening?

1
  • 4
    let cell1 = ... instead of cell1 = ....
    – Zeta
    Commented Oct 31, 2014 at 15:28

1 Answer 1

4

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.

7
  • Apart from a redundant set of parentheses it is exactly the official desugaring from the language report. Of course the compiler won't stop at that stage... Commented Oct 31, 2014 at 16:35
  • @ØrjanJohansen I had just assumed that it might have been something a bit different, since let bindings can use values extracted from monadic actions (do { x <- m; let y = f x; return y }, for example).
    – bheklilr
    Commented Oct 31, 2014 at 16:40
  • Okay, then how do I call the function in the command line? Like I run the program file, then do I just do: test 2 0.4? Because this doesn't return me any result. Commented Oct 31, 2014 at 19:53
  • @EricaMaine Are you wanting to compile it or load it in GHCi? And this function won't return anything because you haven't told it to, you've only told it to print those values.
    – bheklilr
    Commented Oct 31, 2014 at 19:55
  • 1
    @EricaMaine The problems that you're having with your code are simple to solve, but would take more words than can fit in a comment box. I would encourage you to post a new question so that you can paste your code and the full text of the error messages you're seeing, and because what you're asking is a different question entirely than the one asked above. I will give you the hint that your problem will require some manual type conversions and you would have caught the problem earlier had you given you test function an explicit type signature.
    – bheklilr
    Commented Oct 31, 2014 at 20:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.