{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

09_ExamplePrograms

# 09_ExamplePrograms - Exampleprograms YorkUniversity...

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

Example programs York University Department of Computer Science and Engineering York University CSE 3401 V. Movahedi 1 09_ExamplePrograms

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

View Full Document
Overview Classifying terms Weight Conversion Working with Lists Working with Structures Board Example Linked Lists Binary Trees [ref.: Clocksin, Chap 6 & 7 ] [also Prof. Zbigniew Stachniak’s notes] York University CSE 3401 V. Movahedi 2 09_ExamplePrograms
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

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

View Full Document
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
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

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

View Full Document
Working with Lists 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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 24

09_ExamplePrograms - Exampleprograms YorkUniversity...

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

View Full Document
Ask a homework question - tutors are online