如何定义Haskell中的“块”粒度? [英] How is 'block' granularity in Haskell defined?

查看:113
本文介绍了如何定义Haskell中的“块”粒度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到这在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函数绑定中说,


请注意,定义函数的所有子句必须是连续的,并且每个子句中的
个模式数必须相同。与每场比赛相对应的一组
模式必须是线性的---没有变量是
允许在整个组合中多次出现。


您有四个顶级声明:
$ b bar

  bar = 3 

foo

的两个声明
$ b

  foo [] = [] 
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 and where 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 Expressions

Let expressions have the general form let { d1 ; ... ; dn } in e, and introduce a nested, lexically-scoped, mutually-recursive list of declarations (let is often called letrec in other languages). The scope of the declarations is the expression e and the right hand side of the declarations. Declarations are described in Chapter 4.

Besides function bindings and pattern bindings, a let or where can also introduce type signatures and fixity declarations.

这篇关于如何定义Haskell中的“块”粒度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆