简体   繁体   English

Haskell中的交织函数定义

[英]Interleaving function definitions in Haskell

I'm writing a couple pattern-matching functions that recurse on each other, and I would like to be able to interleave their definitions like 我正在编写一对相互递归的模式匹配函数,并且我希望能够像

recA [pattern ...] = [.. something that might call recB with the next pattern ..]
recB [pattern ...] = ...
recA [other ...]   = ...
...b

etc. Is this possible? 等。这可能吗? Is there some more idiomatic alternative? 还有其他惯用的选择吗?

The Haskell 2010 report, section 4.4.3.1 Function Bindings says that Haskell 2010报告第4.4.3.1节“ 函数绑定”指出:

Note that all clauses defining a function must be contiguous, and the number of patterns in each clause must be the same. 请注意,所有定义函数的子句必须是连续的,并且每个子句中的模式数必须相同。

So you can't interleave recA and recB as in your example. 因此,您不能像示例中那样对recArecB进行交织。

There's no theoretical reason (that I can see) why the compiler shouldn't be able to group the various clauses together; 没有理论上的原因(我可以看到),为什么编译器不能将各个子句组合在一起。 I suspect this rule was introduced to guard against human error and confusion. 我怀疑引入此规则是为了防止人为错误和混乱。 As a silly example, what's wrong with the following? 举一个愚蠢的例子,以下是什么问题?

function1 [] = "a"
functionl (x:xs) = "b"
function1 (x:y:xs) = "c"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM