模式匹配Haskell高阶函数? [英] Pattern match Haskell Higher order function?

查看:113
本文介绍了模式匹配Haskell高阶函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个自定义类型和两个函数:

  type MyType = Int  - > Bool 

f1 :: MyType - > Int
f3 :: MyType - > MyType - > MyType

我尝试模式匹配如下:

  f1(f3 abi)= 1 

失败,错误:解析模式错误:f1 。什么是正确的方式来做到上述??基本上,我想知道有多少f3(作为a和b可能是f3或其他函数)。 不能在一个函数上进行模式匹配。对于(几乎)任何给定的函数,都有无数的方法来定义相同的函数。事实证明,计算机总是能够说出给定定义是否表达与另一个定义相同的功能,这在数学上是不可能的。这也意味着Haskell将无法可靠地判断函数是否匹配模式;所以语言根本不允许它。



模式必须是单个变量构造函数适用于其他一些模式。记住构造函数以大写字母和变量开头以小写字母开头,您的模式 f3 a n i 无效;模式 f3 的头是一个变量,但它也适用于 a n i 。这是你收到的错误信息。



因为函数没有构造函数,所以只能使用匹配函数的模式是一个单变量;它匹配所有的函数(无论如何都是传递给模式的正确类型的函数)。这就是Haskell如何强制执行不针对函数的模式匹配规则。基本上,在一个更高阶的函数中,除了之外,没有办法根据你给出的函数告诉任何东西,除了将它应用到某个东西上并查看它的作用。



函数 f1 的类型为 MyType - > INT 。这相当于(Int - > Bool) - > INT 。因此,它需要一个类型为 Int <>的单个函数参数。布尔。我希望 f1 的公式看起来像:

  f1 f = ... 

您不需要检查它是否是 Int - > Bool 函数通过模式匹配;



你不能告诉它是哪一个;但是这通常是把一个函数作为参数的一个要点(因此调用者可以选择任何他们喜欢的函数,因为他们知道你会以同样的方式使用它们)。



我不确定您的意思是我想知道有多少f3。 f1 总是收到一个 single 函数,而 f3 不是正确类型的函数被传递给 f1 (它是一个 MyType - > MyType - > MyType ,而不是 MyType )。


Imagine I have a custom type and two functions:

type MyType = Int -> Bool

f1 :: MyType -> Int
f3 :: MyType -> MyType -> MyType

I tried to pattern match as follows:

f1 (f3 a b i) = 1

But it failed with error: Parse error in pattern: f1. What is the proper way to do the above?? Basically, I want to know how many f3 is there (as a and b maybe f3 or some other functions).

解决方案

You can't pattern match on a function. For (almost) any given function, there are an infinite number of ways to define the same function. And it turns out to be mathematically impossible for a computer to always be able to say whether a given definition expresses the same function as another definition. This also means that Haskell would be unable to reliably tell whether a function matches a pattern; so the language simply doesn't allow it.

A pattern must be either a single variable or a constructor applied to some other patterns. Remembering that constructor start with upper case letters and variables start with lower case letters, your pattern f3 a n i is invalid; the "head" of the pattern f3 is a variable, but it's also applied to a, n, and i. That's the error message you're getting.

Since functions don't have constructors, it follows that the only pattern that can match a function is a single variable; that matches all functions (of the right type to be passed to the pattern, anyway). That's how Haskell enforces the "no pattern matching against functions" rule. Basically, in a higher order function there's no way to tell anything at all about the function you've been given except to apply it to something and see what it does.

The function f1 has type MyType -> Int. This is equivalent to (Int -> Bool) -> Int. So it takes a single function argument of type Int -> Bool. I would expect an equation for f1 to look like:

f1 f = ...

You don't need to "check" whether it's an Int -> Bool function by pattern matching; the type guarantees that it will be.

You can't tell which one it is; but that's generally the whole point of taking a function as an argument (so that the caller can pick any function they like knowing that you'll use them all the same way).

I'm not sure what you mean by "I want to know how many f3 is there". f1 always receives a single function, and f3 is not a function of the right type to be passed to f1 at all (it's a MyType -> MyType -> MyType, not a MyType).

这篇关于模式匹配Haskell高阶函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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