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

如何使用SWI-Prolog的./2函数?求使用示例及该运算符名称

Hey there! Let's clear up the confusion around the ./2 operator in SWI-Prolog and break down what you need to know, since searching for just "." doesn't give meaningful results. I'll cover two key scenarios based on your reference to the F.3 arithmetic functions chapter:

1. The ./2 List Constructor Operator

Official Name: List Constructor

This is the low-level operator that underpins Prolog lists—you're probably more familiar with its syntax sugar [Head|Tail], but ./2 is the explicit, underlying form.

Usage Examples:

  • Create a single-element list:
    ?- .(banana, []).
    % Output: [banana]
    
  • Build a multi-element list step-by-step:
    ?- .(cat, .(dog, .(bird, []))).
    % Output: [cat, dog, bird]
    
  • Verify equivalence with list syntax sugar:
    ?- .(x, [y, z]) = [x|y, z].
    % Output: true
    

2. The /2 Floating-Point Division Operator (Likely What You're Looking For)

Official Name: Floating-Point Division

If you were digging into the F.3 arithmetic functions chapter, this is almost certainly the operator you meant (it's common to refer to operators as Operator/Arity, so /2 is the correct notation for binary division, not ./2). It returns a floating-point result, unlike integer division operators like //2 or div/2.

Usage Examples:

  • Basic division with a whole-number result:
    ?- Result is 8 / 2.
    % Output: Result = 4.0
    
  • Division with a non-integer result:
    ?- Result is 10 / 3.
    % Output: Result = 3.3333333333333335
    
  • Compare with integer division to see the difference:
    ?- FloatResult is 10 / 3, IntResult is 10 // 3.
    % Output: FloatResult = 3.3333333333333335, IntResult = 3
    

内容的提问来源于stack exchange,提问作者Guy Coder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:08:41