在Prolog中运行main时遇Undefined procedure: main/1错误求助
Hey there, let's break down why you're seeing that error and how to fix it.
The Root Cause
That ERROR: Undefined procedure: main/1 message is a bit misleading at first—your code defines a parameterless main/0 predicate, but the error suggests Prolog is looking for a main with 1 argument. The real issue is syntax errors in your code that prevented main/0 from being compiled properly in the first place.
Step-by-Step Fixes
1. Replace Full-Width Quotes with Half-Width
Looking at your code, you used Chinese full-width single quotes (’) around houses.txt:
open(’houses.txt’,read,Str) % Wrong: full-width quotes
Prolog doesn't recognize these as valid string delimiters, which breaks the entire main predicate. Swap them for English half-width quotes (either single ' or double "):
open('houses.txt', read, Str) % Correct: half-width quotes
2. Corrected Full Code
Here's the fully fixed version of your code, with proper spacing and valid syntax:
main:- open('houses.txt', read, Str), read_houses(Str, Houses), close(Str), write(Houses), nl. read_houses(Stream, []):- at_end_of_stream(Stream). read_houses(Stream, [X|L]):- \+ at_end_of_stream(Stream), read(Stream, X), read_houses(Stream, L).
3. Verify Execution
Make sure houses.txt is in the same folder as your flp18-log.pl file, then:
- Run
swipl -s flp18-log.plin your terminal to load the code - At the Prolog prompt, type
?- main.and hit enter
You should see the output:
[gryffindor,hufflepuff,ravenclaw,slytherin] true.
Quick Debug Tip
If you still run into issues, check if main was actually loaded by typing:
?- listing(main).
If it returns the definition of main/0, you're good to go. If it says "undefined procedure", double-check for typos, missing periods, or invalid characters in your code.
内容的提问来源于stack exchange,提问作者user7303261

