문제

I want to have access to a let-binding within a following where-clause.

Why does this Code fail? And even more important: How do I get it to work?

test bar = let baz = bar - 1 in
 foo where
  foo = baz`

it says: Not in scope: baz' Perhaps you meantbar'

도움이 되었습니까?

해결책

You can think of a where as another way to write a let around the right hand sides that belong to a single match.

In your case, it is quite easy, as there is only a single right hand side. Hence, you wrote:

test bar = let foo = baz in let baz = bar-1 in foo

This is not going to work. I recommend to stick with either where or let, most idiomatic would be:

test bar = foo
    where
       foo = baz
       baz = bar -1

Apart from that, when you only define baz then baz' will never be in scope.

다른 팁

where clauses can only access the parameters of the enclosing function and any higher-level bindings. You can pass baz as a parameter to foo:

test bar = let baz = bar - 1 in (foo baz)
  where foo x = x
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top