哈斯克尔:哪里与让 [英] Haskell: Where vs. Let
问题描述
我是Haskell的新手,我非常困惑 Where 与让。他们似乎都提供了类似的目的。我已阅读哪里与让之间的一些比较,但我无法辨别何时使用每个比较。是否有人可以提供一些上下文,或者可以举几个例子说明何时使用另一种情况?
Where VS Let
A
其中
子句只能在函数定义的级别定义。通常,这与let
定义的范围相同。 唯一的区别是当警卫被使用时。其中
子句涵盖所有警卫的范围。相反,let
表达式的范围只是当前的函数子句和守卫(如果有的话)。
Haskell Wiki 非常详细,并提供了各种案例,但它使用了假设的示例。我发现它对于初学者的解释过于简单。
让的优势:
f :: State sa
f = State $ \x - > y
其中y = ... x ...
将不起作用,因为where引用了
模式匹配f =,其中没有x
在范围内。相反,如果您有
开始让,那么您不会
有麻烦。
< a href =http://www.haskell.org/haskellwiki/Let_vs._Where#Advantages_of_let =noreferrer> Haskell Wiki关于Let的优点
f :: State sa
f = State $ \x - >
let y = ... x ...
in y
Where的优势:
fx
| cond1 x = a
| cond2 x = g a
|否则= f(h x a)
其中
a = w x
f x
= let a = w x
在
_ cond1 x = a
| cond2 x = g a
|否则= f(hxa)
Haskell wiki提到 子句是声明性的,而让表达式是表达性的。除了风格,他们的表现如何不同?
声明风格|表达式样式
-------------------------------------- + ---- -----------------------------------------
where子句|让表达式
参数LHS:f x = x * x | Lambda抽象:f = \ x - > x * x
模式匹配:f [] = 0 |情况表达式:f xs = []的情况xs - > 0
近卫:f [x] | x> 0 ='a'|如果表达式:f [x] = if x> 0,那么'a'else ...
- 在第一个示例中,为什么让在作用域中,而其中不是?
- 在第一个示例中应用 Where ?
- 是否可以将这个应用于其中变量代表实际表达式的实例?
- 什么时候使用每个规则有一个通用的经验法则?
更新
f =州f'
f'x = y
其中y = ... x ...
或者像这样:
f = f'
其中
f 'x = y
其中y = ... x ...
<3>:这里是一个没有 ...
的完整示例:
module StateExample其中
data State as = State(s - >(a,s))
f1 :: State Int(Int,Int)
f1 = State $ \state @(a,b) - >
let
hypot = a ^ 2 + b ^ 2
result =(hypot,state)
结果
f2 :: State Int(Int ,Int)
f2 =状态f
其中
f state @(a,b)= result
其中
hypot = a ^ 2 + b ^ 2
result =(hypot,state)
4:何时使用 let
或其中
是品味的问题。我使用 let
来强调一个计算(通过将它移到前面)和其中
强调程序流移动计算到后面)。
I am new to Haskell and I am very confused by Where vs. Let. They both seem to provide a similar purpose. I have read a few comparisons between Where vs. Let but I am having trouble discerning when to use each. Could someone please provide some context or perhaps a few examples that demonstrate when to use one over the other?
Where vs. Let
A
where
clause can only be defined at the level of a function definition. Usually, that is identical to the scope oflet
definition. The only difference is when guards are being used. The scope of thewhere
clause extends over all guards. In contrast, the scope of alet
expression is only the current function clause and guard, if any.
The Haskell Wiki is very detailed and provides various cases but it uses hypothetical examples. I find its explanations too brief for a beginner.
Advantages of Let:
f :: State s a
f = State $ \x -> y
where y = ... x ...
will not work, because where refers to the pattern matching f =, where no x is in scope. In contrast, if you had started with let, then you wouldn't have trouble.
Haskell Wiki on Advantages of Let
f :: State s a
f = State $ \x ->
let y = ... x ...
in y
Advantages of Where:
f x
| cond1 x = a
| cond2 x = g a
| otherwise = f (h x a)
where
a = w x
f x
= let a = w x
in case () of
_ | cond1 x = a
| cond2 x = g a
| otherwise = f (h x a)
The Haskell wiki mentions that the Where clause is declarative while the Let expression is expressive. Aside from style how do they perform differently?
Declaration style | Expression-style
--------------------------------------+---------------------------------------------
where clause | let expression
arguments LHS: f x = x*x | Lambda abstraction: f = \x -> x*x
Pattern matching: f [] = 0 | case expression: f xs = case xs of [] -> 0
Guards: f [x] | x>0 = 'a' | if expression: f [x] = if x>0 then 'a' else ...
- In the first example why is the Let in scope but Where is not?
- Is it possible to apply Where to the first example?
- Can some apply this to real examples where the variables represent actual expressions?
- Is there a general rule of thumb to follow when to use each?
Update
For those that come by this thread later on I found the best explanation to be found here: "A Gentle Introduction to Haskell".
Let Expressions.
Haskell's let expressions are useful whenever a nested set of bindings is required. As a simple example, consider:
let y = a*b f x = (x+y)/y in f c + f d
The set of bindings created by a let expression is mutually recursive, and pattern bindings are treated as lazy patterns (i.e. they carry an implicit ~). The only kind of declarations permitted are type signatures, function bindings, and pattern bindings.
Where Clauses.
Sometimes it is convenient to scope bindings over several guarded equations, which requires a where clause:
f x y | y>z = ... | y==z = ... | y<z = ... where z = x*x
Note that this cannot be done with a let expression, which only scopes over the expression which it encloses. A where clause is only allowed at the top level of a set of equations or case expression. The same properties and constraints on bindings in let expressions apply to those in where clauses. These two forms of nested scope seem very similar, but remember that a let expression is an expression, whereas a where clause is not -- it is part of the syntax of function declarations and case expressions.
1: The problem in the example
f :: State s a
f = State $ \x -> y
where y = ... x ...
is the parameter x
. Things in the where
clause can refer only to the parameters of the function f
(there are none) and things in outer scopes.
2: To use a where
in the first example, you can introduce a second named function
that takes the x
as a parameter, like this:
f = State f'
f' x = y
where y = ... x ...
or like this:
f = State f'
where
f' x = y
where y = ... x ...
3: Here is a complete example without the ...
's:
module StateExample where
data State a s = State (s -> (a, s))
f1 :: State Int (Int, Int)
f1 = State $ \state@(a, b) ->
let
hypot = a^2 + b^2
result = (hypot, state)
in result
f2 :: State Int (Int, Int)
f2 = State f
where
f state@(a, b) = result
where
hypot = a^2 + b^2
result = (hypot, state)
4: When to use let
or where
is a matter of taste. I use let
to emphasize a computation (by moving it to the front) and where
to emphasize the program flow (by moving the computation to the back).
这篇关于哈斯克尔:哪里与让的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!