Skip to main content

Operators and Arithmetic

  • Chapter
  • First Online:
Logic Programming with Prolog
  • 3306 Accesses

Abstract

This chapter introduces operator notation for predicates and describes the operators provided for evaluating and comparing the values of arithmetic expressions, for testing for equality of either arithmetic expressions or terms and for testing for the negation of a goal or the disjunction of two goals.

After reading this chapter you should be able to:

  • Convert unary and binary predicates to operators

  • Evaluate arithmetic expressions and compare their values

  • Test for equality of arithmetic expressions and terms

  • Use the ’not’ and disjunction operators

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

eBook
USD 16.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 16.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Author information

Authors and Affiliations

Authors

Practical Exercise 4

Practical Exercise 4

  1. (1)

    This program is based on Animals Program 3, given in Chapter 2.

dog(fido). large(fido).

cat(mary). large(mary).

dog(rover). small(rover).

cat(jane). small(jane).

dog(tom). small(tom).

cat(harry).

dog(fred). large(fred).

cat(henry). large(henry).

cat(bill).

cat(steve). large(steve).

large(jim).

large(mike).

large_dog(X):- dog(X),large(X).

small_animal(A):- dog(A),small(A).

small_animal(B):- cat(B),small(B).

chases(X,Y):-

    large_dog(X),small_animal(Y),

    write(X),write(’ chases ’),write(Y),nl.

Convert the seven predicates used to operator form and test your revised program. The output should be the same as the output from the program above. Include directives to define the operators in your program.

  1. (2)

    Define and test a predicate which takes two arguments, both numbers, and calculates and outputs the following values: (a) their average, (b) the square root of their product and (c) the larger of (a) and (b).

Rights and permissions

Reprints and permissions

Copyright information

© 2013 Springer-Verlag London

About this chapter

Cite this chapter

Bramer, M. (2013). Operators and Arithmetic. In: Logic Programming with Prolog. Springer, London. https://doi.org/10.1007/978-1-4471-5487-7_4

Download citation

  • DOI: https://doi.org/10.1007/978-1-4471-5487-7_4

  • Published:

  • Publisher Name: Springer, London

  • Print ISBN: 978-1-4471-5486-0

  • Online ISBN: 978-1-4471-5487-7

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics