Haskell: Where vs. Let

前端 未结 5 1685
余生分开走
余生分开走 2020-12-02 04:56

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 bet

5条回答
  •  忘掉有多难
    2020-12-02 05:30

    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).

提交回复
热议问题