Skip to main content

Input and Output

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

Abstract

Describes the principal built-in predicates available for both term by term and character by character input and output and for reading and writing files. Also introduces the notion of the ASCII value of a character.

After reading this chapter you should be able to:

  • Use the built-in predicates that read from and write to either the user’s terminal (keyboard and screen) or a file, both term by term and character-by-character in your own programs

  • Use ASCII values to manipulate strings of characters

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

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 49.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 5

Practical Exercise 5

  1. (1)

    Define a predicate makelower/0 which reads in a line of characters from the keyboard and outputs it again as a single line with any upper case letters converted to lower case. (The ASCII values of the characters a, z, A and Z are 97, 122, 65 and 90, respectively.)

Thus the following would be a typical use of makelower:

  • ?- makelower.

  • |: This is an Example 123 inCLUDing numbers and symbols +−*/@[] XYz

  • this is an example 123 including numbers and symbols +−*/@[] xyz

  1. (2)

    Define a predicate copyterms which reads all the terms in a text file and outputs them as terms to another text file one by one on separate lines.

The output file should be in a format suitable for use as the input file in a subsequent call of copyterms. Thus for example if the input file contained

’first term’. ’second term’.’third term’. fourth. ’fifth term’. sixth.

The output file would contain

’first term’. ’second term’. ’third term’. fourth. ’fifth term’. sixth.

  1. (3)

    Create a text file testa.txt containing two lines, each of five characters followed by a new line, e.g.

abcde fghij

Define a predicate readfile that will read thirteen characters from this file one by one and output the ASCII value of each character. Use this to establish whether the representations of ’end of file’ and ’end of record’ for your version of Prolog are as suggested in Sections 5.10.1 and 5.10.2, respectively.

  1. (4)

    Using a text editor, create two text files in1.txt and in2.txt, each comprising a number of terms terminated by end.

Define and test a predicate combine that takes the names of two input files as its first two arguments and the name of an output file as its third argument. The output file should contain the terms in the first input file followed by the terms in the second, one per line and terminated by end.

  1. (5)

    Define and test a predicate compare that reads in two text files term by term and for each pair of corresponding terms outputs a message either saying that they are the same or that they are different. Assume that both files contain the same number of terms and that the final term in each is end.

Rights and permissions

Reprints and permissions

Copyright information

© 2013 Springer-Verlag London

About this chapter

Cite this chapter

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

Download citation

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

  • 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