Вопрос

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