Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

prolog help

Name: Anonymous 2012-05-03 22:56

So I was on this thread on /g/ where everybody was trying to solve the fizzbuzz problem in various languages, and I thought I'd try it in prolog. It seemed easy, but I couldn't even begin to solve it without causing errors or loops that I can't determine the cause of. If anyone isn't familiar, the goal is that for every number i in 1-100, if i is divisible by 3 you write
"fizz", 5 you write "buzz" (both if divisible by 15), or the value of i if neither. So I started with:

fizz(3). fizz(X) :- fizz(Y),X is Y + 3.
buzz(5). buzz(X) :- buzz(Y),X is Y + 5.

When I try to unify with "fizz(X),buzz(X)." it hangs and doesn't give me anything. Anyone know why?

Name: Anonymous 2012-05-04 0:13

Anybody? Someone's gotta know the answer.

Name: Anonymous 2012-05-04 5:51

>>1,2
Yes.

Name: Anonymous 2012-05-04 7:35

So the stories were true.

Name: Anonymous 2012-05-04 14:18

>>3
If you know, then why not share?
>>4
Care to expand on that?

Name: Anonymous 2012-05-04 14:37

i don't know how to fix it, prolog is hard
do you know that you can use the trace/0 built-in predicate to step through the execution of your program one step at a time?
i can see that the problem is that it recurses forever. Y is X - 3 might work better if you can find a way to make sure that the rhs is always instantiated with it.

Name: Anonymous 2012-05-04 14:53

Prolog is only hard if you try to get it to do things it wasn't meant to do. Like imperative programming.
It can do one thing very well, and everything else merely in principle. Don't use it for Fizzbuzz.

Name: bampu pantsu 2012-05-29 4:45

bampu pantsu

Don't change these.
Name: Email:
Entire Thread Thread List