You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Prolog中运行main时遇Undefined procedure: main/1错误求助

Fixing "Undefined procedure: main/1" in Your Prolog Code

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.pl in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:50:54