09_ExamplePrograms

09_ExamplePrograms - Exampleprograms YorkUniversity

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Example programs York University Department of Computer Science and Engineering York University CSE 3401 V. Movahedi 1 09_ExamplePrograms
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Overview Classifying terms Weight Conversion Working with Lists Working with Structures Board Example Linked Lists Binary Trees [also Prof. Zbigniew Stachniak’s notes] York University CSE 3401 V. Movahedi 2 09_ExamplePrograms
Background image of page 2
Weight conversion Problem: Convert Pounds to Kilos and vice versa Show an error message and fail if no inputs given Show an error message and fail if given input is not a number Some useful built in predicates: York University CSE 3401 V. Movahedi 3 var(X) succeeds if X is a variable and is not instantiated nonvar(X) succeeds if var(X) fails atom(X) succeeds if X stands for an atom e.g. adam, ‘George’, . .. number(X) succeeds if X stands for a number atomic(X) succeeds if X stands for a number or an atom integer(X) Succeeds if X stands for an integer. 09_ExamplePrograms
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Weight Conversion code convert (Pounds, Kilos):- % If no inputs given var(Pounds), var(Kilos), !, write('No inputs!'), nl, fail. convert (Pounds, _):- % If Pounds is known, but not a number nonvar(Pounds), \+number(Pounds), !, write('Inputs must be numbers!'), nl, fail. convert (_, Kilos):- % If Kilos is known, but not a number nonvar(Kilos), \+number(Kilos), !, write('Inputs must be numbers!'), nl, fail. convert (Pounds, Kilos):- % If Pounds is known number(Pounds), !, Kilos is Pounds * 0.45359. convert (Pounds, Kilos):- % Otherwise Pounds is Kilos/0.45359. York University CSE 3401 V. Movahedi 4 09_ExamplePrograms
Background image of page 4
Weight conversion queries ?- convert(X,Y). No inputs! false. ?- convert(20,Y). Y = 9.0718. ?- convert(X,9). X = 19.8417. ?- convert(20,9.0718). true. ?- X=5, convert(X,Y). X = 5, Y = 2.26795. ?- convert(X,a). Inputs must be numbers! false. York University CSE 3401 V. Movahedi 5 09_ExamplePrograms
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Find the first element of a list. first (X, [X|_]). Find the last element of a list. last (X, [X]). last (X, [H|T]) :- last(X, T). Shift the elements of a list to left. lshift
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/14/2012 for the course CSE 3401 taught by Professor Movahedi during the Fall '11 term at York University.

Page1 / 24

09_ExamplePrograms - Exampleprograms YorkUniversity

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online