then part test then part else part

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: t are divisible by 2 Let’s use Prolog’s built-in arithmetic evaluation predicate is/2! –  ?- Y is 3/2.! •  produces a floating point number –  Y=1.5! –  ?- Y is 4/2.! •  produces an integer –  Y=2! –  ?- Y is round(3/2).! •  –  rounds up to the nearest integer Y=2 !! •  Define is_even(X) :-! Y is X/2,! Z is round(X/2),! Y = Z. Odd Numbers Even numbers Odd numbers •  •  Idea: –  to test whether a number X is even 1.  divide it by 2, call this number Y 2.  divide it by 2 and round it, call this number Z 3.  X is even if Y=Z. •  Define is_even(X) :-! Y is X/2,! Z is round(X/2),! Y = Z. Define is_odd(X) :-! Y is X/2,! Z is round(X/2),! \+ Y = Z. •  or define is_odd(X) :- \+ is_even(X).! Output •  You can use built-in predicates –  write/1 and –  nl/1 to print values of variables etc. •  Exam...
View Full Document

This note was uploaded on 04/29/2012 for the course LING 388 taught by Professor Staff during the Fall '08 term at Arizona.

Ask a homework question - tutors are online