Haskell: Where vs. Let

前端 未结 5 1672
余生分开走
余生分开走 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:25

    Legal:

    main = print (1 + (let i = 10 in 2 * i + 1))
    

    Not legal:

    main = print (1 + (2 * i + 1 where i = 10))
    

    Legal:

    hasVowel [] = False
    hasVowel (x:xs)
      | x `elem` vowels = True
      | otherwise = False
      where vowels = "AEIOUaeiou"
    

    Not legal: (unlike ML)

    let vowels = "AEIOUaeiou"
    in hasVowel = ...
    

提交回复
热议问题