模式匹配 - Prolog vs. Haskell [英] Pattern Matching - Prolog vs. Haskell

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

问题描述

这不是家庭作业问题,而是考试学习指南问题.Prolog 与 Haskell 中的模式匹配有什么区别?

This is not a homework question, rather an exam study guide question. What is the difference between pattern matching in Prolog Vs Haskell?

我已经进行了一些研究并阅读了它们背后的理论并没有真正让我对两者之间有一个扎实的理解.我在Prolog中读到,模式匹配是不同的,因为它具有统一变量的能力,因此能够通过分辨率进行推断并吐出可能的答案

I've done some research and reading up on the theories behind them doesn't really give me a solid understanding between the two. I read that in Prolog, pattern matching is different because it has the ability to unify variables and thus be able to deduce through resolution and spit out the possible answer

eg ?- [a,b] = [a,X]
   X = b

现在我不确定如何在 Haskell 中显示模式匹配.我知道上面在 Prolog 中显示的相同查询在 Haskell 中不起作用,因为 Haskell 不能像 Prolog 那样统一.我记得在某个地方,要在 Haskell 中得到相同的答案,你必须通过警卫明确地告诉它.

Now I'm not sure how to display pattern matching in Haskell. I know that the same query above shown in Prolog will not work in Haskell because Haskell cannot unify like Prolog. I remember somewhere that to get the same answer in Haskell, you have to explicitly tell it through guards.

我知道我非常接近理解它,但我需要有人帮我分解 Barney 风格,这样我才能完全理解它并向 12 岁的孩子解释它.这一直困扰着我很长一段时间,我似乎找不到可靠的解释.

I know that I am pretty close to understanding it, but I need someone to break it down Barney style for me so I can FULLY understand it and explain it to a 12 year old. This has been bugging me for quite some time and I can't seem to find a solid explanation.

顺便说一下,上面显示的示例只是向你们展示我到目前为止所学到的知识,并且我实际上正在尝试寻找答案.我的主要问题与上面的示例无关,而是对两者之间的区别的完整理解.

By the way the example shown above was just to display to you guys what I've learned so far and that I'm actually trying to find an answer. My main question does not relate to the examples above but rather a complete understanding on the difference between the two.

推荐答案

Prolog 模式匹配是基于统一的,特别是 Martelli-Montanari 算法(减去发生检查,默认情况下).该算法匹配相同位置的值,将一侧的变量绑定到另一侧相应位置的值.这种模式匹配可以双向工作,因此在 Prolog 中,您可以使用参数作为输入和输出.一个简单的例子,length/2 谓词.我们可以使用它来(评论解释查询):

Prolog pattern matching is based on unification, specifically the Martelli-Montanari Algorithm (minus the occurs check, by default). This algorithm matches values of the same position, binding variables on one side to a value at corresponding position on the other side. This kind of pattern matching could work both ways, therefore in Prolog you could use arguments as both input and output. A simple example, the length/2 predicate. We could use this to (comment explains the query):

?- length([],0).      % is the length of empty list zero?
?- length([a,b,c],X). % what's the length of list consisting of a,b and c?
?- length(L,5).       % give me all lists that have length of 5

Haskell 模式匹配 是一种单向匹配,将变量绑定到不同部分给定值.一旦绑定,它就会执行相应的操作(右侧).例如,在函数调用中,模式匹配可以决定调用哪个函数.例如:

Haskell pattern matching is a one way matching, to bind variables to different parts of given value. Once bound, it carries out the corresponding action (the right hand side). For example, in a function call, pattern matching may decide which function to call. e.g.:

sum []     = 0
sum (x:xs) = x + sum xs

第一个 sum 绑定空列表,而第二个绑定至少 1 个元素的列表.基于此,给定 sum <a list>,结果可能是 0x + sum xs,具体取决于 sum <a list> 匹配 sum []sum (x:xs).

the first sum binds empty list, while the second binds a list of at least 1 element. Based on this, given sum <a list>, the result could be either 0 or x + sum xs depending on whether sum <a list> matches sum [] or sum (x:xs).

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

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