TaoPR
TaoPR

Reputation: 6052

How to use let or where statement within binding

I'm writing hspec test suite which uses binding as follows:

it "should take the x component" $ do
    (getX $ coord $ [1,0,2]) `shouldBe` 1

However, for more complicated statement, one-liner is not conveniently readable so I try to strip the variable out in where expression:

it "should take the x component" $ do
    (getX c) `shouldBe` 1
       where c = coord $ [1,0,2]

But this turns out illegal statement in Haskell. Using let does not succeed either.

it "should take the x component" $ do
    let c = coord $ [1,0,2]
    in (getX c) `shouldBe` 1

How can I assign a helper variable for the same purpose as let or where within do binding? Exploring the documentations but can't find a good example though.

Upvotes: 2

Views: 93

Answers (2)

Samee
Samee

Reputation: 846

let statements inside do do not need the in keyword. In other words, try this:

it "should take the x component" $ do
    let c = coord $ [1,0,2]
    (getX c) `shouldBe` 1

Upvotes: 4

dfeuer
dfeuer

Reputation: 48591

It's hard to say why the first one fails without seeing more context. The second fails because let works differently in do blocks. Either indent the in another couple spaces or delete the keyword in and make the rest of the line align with the keyword let.

Upvotes: 4

Related Questions