haskell - where block 的语法

标签 haskell syntax notation

我正在阅读 Graham Hutton 的《Haskell 编程》,它在第 13 章中给出了以下代码:

import Control.Applicative
import Data.Char

{- some code omitted -}

newtype Parser a = P (String -> [(a, String)])

item :: Parser Char
item = P (\ input -> case input of
                     []   -> []
                     x:xs -> [(x,xs)])

three :: Parser (Char,Char)
three = pure g <*> item <*> item <*> item
        where g a b c = (a,c)

我很难理解最后一行

where g a b c = (a,c)

我知道这条线存在是因为 three 的类型是 Parser(Char, Char) 但是 g a b c 代表什么? g a b c 在语法上如何有效?我习惯于在类似情况下看到哪里

f :: s -> (a,s)
f x = y
   where y = ... x ...

其中每个符号 x 和 y 出现在 where 声明之前。

最佳答案

这是声明函数的语法。它相当于

 where g = \a b c -> (a,c)

g 是一个接受 3 个参数并返回一个元组的函数

关于haskell - where block 的语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45125904/

相关文章:

windows - Windows 安装程序上的 Haskell

haskell - 如何从具有函数依赖的类型类中获取和使用依赖类型?

c++ - C/C++ : What does this syntax mean?

ruby - 在 Ruby 中使用范围填充数组的正确方法

c++ - 中缀表达式评估

algorithm - 证明 f(n) + d(n)= O(g(n)+ h(n))

haskell - 尝试升级 Haskell 堆栈

haskell - 弱引用终结器保证运行

ruby:此语法的名称,它拆分块的位置参数

Python 符号?