Question

I have a problem with the projections in my definition of If Then Else. It's actually executing as 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). ite[0,2,3] should return 3 and ite[1,2,3] should return 2. Instead the opposite is happening. How can I correct this?

Was it helpful?

Solution

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. 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. The same condition used in Boolean algegra. if 0 we have false, otherwise if a number evaluates to anything (0<), we have 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). But for all intents and purposes we can say that the second expression will be executed on (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. 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.

OTHER TIPS

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. Also it make writing and/or/... easer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top