如何定义Haskell中的“块”粒度? [英] How is 'block' granularity in Haskell defined?
问题描述
我注意到这在Haskell中是不合法的:
foo :: [a] - > [a]
foo [] = []
bar = 3
foo(x:xs)= x:foo xs
$然而,这是:
foo :: [a] - > [a]
bar = 3
foo [] = []
foo(x:xs)= x:foo xs
尽管为一个函数匹配的模式必须全部作为一个集合组合在一起,但似乎类型签名可能会漫游。但有多远?我被告知它必须在同一个块......但从技术上讲,究竟是一个块?
解决方案每个函数的声明必须在一起,除了声明可以以任何顺序出现在文件中。
Haskell报告中的声明部分,在4.4.3.1函数绑定中说,
请注意,定义函数的所有子句必须是连续的,并且每个子句中的
个模式数必须相同。与每场比赛相对应的一组
模式必须是线性的---没有变量是
允许在整个组合中多次出现。
您有四个顶级声明:
$ bbar bar = 3
的两个声明
foo
$ bfoo [] = []
foo(x:xs)= x:foo xs
foo
foo :: [a] - > [a]
除了函数绑定的连续声明外,它们可以以任何距离彼此在同一个文件中,仍然具有相同的含义。
let和where
let
和其中
也引入了非顶级声明。在这些声明范围中,除了函数的连续声明外,声明可以以任何顺序出现并且仍然具有相同的含义。在不同范围内做出的声明不一定具有相同的含义。范围在3.15节的 Haskell报告表达式部分中进行了描述。让表达式<让表达式具有一般形式let {d1; ...;在$
和
中引入一个嵌套的,词汇范围相互递归的
声明列表(let
在其他语言中通常被称为letrec
)。声明的
范围是表达式e
和右边
。声明在第4章中描述。
除了函数绑定和模式绑定外,
let
或其中
也可以引入类型签名和固定声明。I noticed that this is not legal in Haskell:
foo :: [a] -> [a] foo [] = [] bar = 3 foo (x:xs) = x : foo xs
However, this is:
foo :: [a] -> [a] bar = 3 foo [] = [] foo (x:xs) = x : foo xs
Though the patterns matched for a function must all be grouped together as a set, it seems the type signature can wander. But how far? I was told it must be in the same "block"... but technically speaking, what exactly is a "block"?
解决方案The declarations for each function must be together, other than that declarations can occur in files in any order.
The declarations section of the Haskell report, in section 4.4.3.1 Functions Bindings, says
Note that all clauses defining a function must be contiguous, and the number of patterns in each clause must be the same. The set of patterns corresponding to each match must be linear---no variable is allowed to appear more than once in the entire set.
You have four top-level declarations:
A declaration for
bar
bar = 3
Two declarations for
foo
foo [] = [] foo (x:xs) = x : foo xs
A type signature for
foo
foo :: [a] -> [a]
Except for the contiguous declarations for function bindings, these can be in any order any distance from each other in the same file and still have the same meaning.
let and where
let
andwhere
also introduce non-top-level declarations. In these declaration scopes, except for the contiguous declarations for functions, declarations can occur in any order and still have the same meaning. Declarations made in different scopes do not necessarily have the same meaning. Scopes are described in the expressions sections of the Haskell report, in section 3.12 Let ExpressionsLet expressions have the general form
let { d1 ; ... ; dn } in e
, and introduce a nested, lexically-scoped, mutually-recursive list of declarations (let
is often calledletrec
in other languages). The scope of the declarations is the expressione
and the right hand side of the declarations. Declarations are described in Chapter 4.Besides function bindings and pattern bindings, a
let
orwhere
can also introduce type signatures and fixity declarations.这篇关于如何定义Haskell中的“块”粒度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!