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

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

问题描述

这不是家庭作业问题,而是考试学习指南问题. Prolog Vs 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模式匹配基于统一,特别是

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 <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与Haskell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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