简体   繁体   English

原始递归If else else实际执行if else else

[英]Primitive Recursion If Then Else actually executing If Else Then

I have a problem with the projections in my definition of If Then Else. 我对If If Else的定义中的预测有疑问。 It's actually executing as If-Else-Then. 它实际上是以If-Else-Then的身份执行的。

 import Prelude hiding (pred,and,or,not)

 data PR = Z
     | S
     | P Int
     | C PR [PR]
     | PR PR PR
     deriving Show
 eval :: PR -> [Integer] - Integer
 eval Z _ = 0
 eval S [x] = x+1
 eval (P n) xs = nth n xs
 eval (C f gs) xs = eval f (map (\g -> eval g xs) gs)
 eval (PR g h) (0:xs) = eval g xs
 eval (PR g h) (x:xs) = eval h ((x-1) : eval (PR g h) ((x-1):xs) : xs)

 nth _ [] = error "nth nil"
 nth 0 _ = error "nth index"
 nth 1 (x:_) = x
 nth (n) (_:xs) = nth (n-1) xs

 one = C S [Z]
 plus = PR (P 1) (C S [P 2])
 ife = PR (P 1) (C (P 2) [P 3, P 4])

If I try swapping P 3 and P 4 it just breaks entirely (returns the 'then' value every time). 如果我尝试交换P 3P 4它将完全中断(每次都返回'then'值)。 ite[0,2,3] should return 3 and ite[1,2,3] should return 2 . ite[0,2,3]应该返回3ite[1,2,3]应该返回2 Instead the opposite is happening. 相反,情况正相反。 How can I correct this? 我该如何纠正?

How are you liking this class? 你觉得这堂课怎么样? I noticed you and I have very similar homework assignments, Very similar. 我注意到您和我有非常相似的作业,非常相似。

Well First off you wanna make a Primitive recursive function that emulates the IF-Then-Else model. 首先,您想创建一个模仿IF-Then-Else模型的基本递归函数。 therefore, 因此,

eval ite [0,1,2] => 1

and

eval ite [1,2,3] => 3

and with what you provided, you seem to be getting a function with the same qualities yet in in the opposite instances depending on the first input. 根据您提供的内容,在相反的情况下(取决于第一个输入),您似乎正在获得具有相同质量的功能。

ife = PR (P 1) (C (P 2) [P 3, P 4])

now what is your function saying? 现在您的功能在说什么? your ITE implementation uses the primitive recursion construct, that's a start, because in this you can split execution to two diffent expressions based on a condition. 您的ITE实施使用原始递归构造,这是一个开始,因为在此情况下,您可以根据条件将执行分为两个不同的表达式。 The same condition used in Boolean algegra. 布尔代数中使用的条件相同。 if 0 we have false, otherwise if a number evaluates to anything (0<), we have true. 如果为0,则为false;否则,如果数字为任意值(0 <),则为true。 The PR construct does this by evaluating its first argument if the head of the "stack" is a 0, otherwise it evaluates its second argument in hopes that somewhere along the line it'll terminate (often time decrementing the head as your counter and eventually execution the first argument). 如果“堆栈”的头为0,PR构造会通过评估其第一个参数来执行此操作,否则它会评估其第二个参数,以期希望沿该行的某个位置终止(通常是将头作为计数器递减,最终执行第一个参数)。 But for all intents and purposes we can say that the second expression will be executed on (0<). 但是出于所有意图和目的,我们可以说第二个表达式将在(0 <)上执行。

Phew! So, how do we fix your implementation!? 那么,我们如何解决您的实施问题呢? Easy: 简单:

ife = PR (P 2) (C (P 1) [P 3, P 4])

We switch your two projections, as you simply had them backwards. 我们将您的两个投影切换,因为您只是将它们向后移动。 If the head of the stack is Z, we want to project the second expression, otherwise we project the first. 如果堆栈的头是Z,我们要投影第二个表达式,否则我们投影第一个表达式。 or better yet: 或更好:

ite = PR (P 2) (P 1)

I think, I'm not done with the homework either and if I'm wrong I would Highly appreciate any extra insight. 我认为,我也没有完成作业,如果我错了,我将不胜感激。

Since I am not good enough to leave a comment I will leave it here. 由于我没有足够的能力发表评论,因此我将其留在这里。

I think it should be 我认为应该

ife = PR (P 1) (C (P 2) [P 3, P 3])
                                ^

instead of the original version 而不是原始版本

ife = PR (P 1) (C (P 2) [P 3, P 4])
                                ^

This way X will be select if the first value is not 0 ELSE Y. This way no random values are returned. 如果第一个值不为0,否则将选择X。否则,将不返回任何随机值。 Also it make writing and/or/... easer. 它还使写作和/或/ ...更加容易。

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

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