Prolog-Fail-Input-Output

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/12

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

13 Terms

1
New cards

What does the write predicate do in Prolog?

The write predicate outputs whatever value is bound to its argument. It doesn’t automatically add newlines, so multiple writes will appear concatenated. Ex: write(likes(Mary, pizza)) displays “likes(Mary,pizza)”

2
New cards

How does nl work in Prolog?

The nl predicate outputs a newline character, moving to the start of the next line. It’s used to format output by breaking concatenated write results.

3
New cards

What is the purpose of the tab predicate?

The tab(N) predicate outputs N spaces, useful for formatting text output. Ex: write(hi), tab(15), write(there) would put 15 spaces between “hi” and “there”.

4
New cards

How do you read input from the keyboard in Prolog?

The read(X) predicate reads a term from keyboard input, binding it to X. Prolog prompts with “|:” and input must be terminated with a period. Ex: ?- read(X). followed by typing likes(fido, biscuits). binds to that term.

5
New cards

How do you write to a file in Prolog?

Use tell(Filename) to redirect output to a file, then use write predicates as normal, and finally told to close the file and restore output to screen. E: tell(‘myfile’), write(hello), nl, told.

6
New cards

How do you read from a file in Prolog?

Use see(Filename) to redirect input from a file, then use predicates as normal, and finally seen to close the file and restore input form keyboard. Ex: see(‘mydata’), read(X), read(Y), seen.

7
New cards

How do you handle end-of-file conditions in Prolog?

When read(Term) reaches end of file, it binds Term to the special atom end_of_file. This can be used to stop processing. Ex: process file :- read(Term), Term \== end_of_file, process1(Term).

8
New cards

How do you load Prolog code form another file?

The consult(File) predicate loads Prolog code from the specified file. Ex: ?- consult(‘my code.pl’).

9
New cards

How can you list all solutions to a query in Prolog?

Use the fail predicate to force backtracking after printing each solution. Ex: capital_of(State, City), write(City), write(‘ is capital of ‘), write(State), nl, fail. will list all capital-state pairs.

10
New cards

What happens when a query contains a fail predicate?

The fail predicate deliberately fails, forcing Prolog to backtrack and find alternative solutions. It’s useful for enumeration patterns when combined with output predicates.

11
New cards

How can you create a named predicate that lists all solutions?

Define a predicate that includes the query pattern with output and the fall predicate. Ex: print_capitals :- capital_of(State, City), write(City), write(‘ is capital of ‘), write(State), nl, fail.

12
New cards

How do you output a single character in Prolog?

The put C predicate writes a single character. C can be a character (in quotes) or an ASCII code (0-255). Ex: put(‘f’), put(105), put(‘d’), put(‘o’) would output “fido”.

13
New cards

How can you hide variable bindings when displaying query results?

Create a subroutine predicate that includes all the variable bindings and output statements. When called, only the output statements will show their results, not the variable bindings. Ex: thing :- likes(Mary, X), likes(John, Y), write(X), nl, write(Y), nl.