理解项 上机操作

转载自:https://mp.weixin.qq.com/s/zK48OWSjL-GUlBm9GmzOEA

?- a==a.

true.

 

?- a==b.

false.

 

?- a=='a'.

true.

 

?- X==Y.

false.

 

?- X=Y.

X = Y.

 

?- a=X,a==X.

X = a.

 

?- X=Y,X==Y.

X = Y.

 

?- a\==a.

false.

 

?- a\==b.

true.

 

?- a\=='a'.

false.

 

?- X\==a.

true.

 

?- X\==Y.

true.

 

?- 2+3 == +(2,3).

true.

 

?- +(2,3) == 2+3.

true.

 

?- 2-3 == -(2,3).

true.

 

?- *(2,3) == 2*3.

true.

 

?- 2*(7+2) == *(2,+(7,2)).

true.

 

?- (2 < 3) == <(2,3).

true.

 

?- (2 =< 3) == =<(2,3).

true.

 

?- (2 =:= 3) == =:=(2,3).

true.

 

?- (2 =\= 3) == =\=(2,3).

true.

 

?- (2 > 3) == >(2,3).

true.

 

?- (2 >= 3) == >=(2,3).

true.

 

?- 2 =:= 3 == =:=(2,3).

ERROR: Syntax error: Operator priority clash

ERROR: 2 =:

ERROR: ** here **

ERROR: = 3 == =:=(2,3) . 

 

?- (2=:=3) == =:=(2,3).

true.

 

?- [a,b,c,d] == [a|[b,c,d]].

true.

 

?- [a,b,c,d] == [a,b|[c,d]].

true.

 

?- [a,b,c,d] == [a,b,c|[d]].

true.

 

?- [a,b,c,d] == [a,b,c,d|[]].

true.

 

// Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)ansi_term.pl:428:13bug

 

$ swipl --traditional

ERROR: /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/library/ansi_term.pl:428:13: Syntax error: Operator expected

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- .(a,[]) == [a].

true.

 

?- .(f(d,e),[]) == [f(d,e)].

true.

 

?- .(a,.(b,[])) == [a,b].

true.

 

?- .(a,.(b,.(f(d,e),[]))) == [a,b,f(d,e)].

true.

 

?- .(.(a,[]),[]) == [[a]].

true.

 

?- .(.(.(a,[]),[]),[]) == [[[a]]].

true.

 

?- .(.(a,.(b,[])),[]) == [[a,b]].

true.

 

?- .(.(a,.(b,[])),.(c,[])) == [[a,b],c].

true.

 

?- .(.(a,[]),.(b,.(c,[]))) == [[a],b,c].

true.

 

?- .(.(a,[]),.(.(b,.(c,[])),[])) == [[a],[b,c]].

true. 

 

?- .(f(d,e),[]) = Y.

Y = [f(d, e)].

 

?- .(a,.(b,[])) = X, Z= .(.(c,[]),[]), W = [1,2,X].

X = [a, b],

Z = [[c]],

W = [1, 2, [a, b]].

 

?- X = a, atom(X).

X = a.

// 为真无输出true

?- X = a, atom(Y).

false. // 为非输出false

 

?- atom(X), X = a.

false.  // 判别时还没合一

 

?- atomic(mia).

true.

 

?- atomic(8).

true.

 

?- atomic(3.25).

true.

 

?- atomic(loves(vincent,mia)).

false.

 

?- atomic(X).

false.

 

?- var(X).

true.

 

?- var(mia).

false.

 

?- var(8)

|    .

false.

 

?- var(3.25).

false.

 

?- var(loves(vincent,mia)).

false.

 

?- nonvar(X).

false.

 

?- nonvar(mia).

true.

 

?- nonvar(8).

true.

 

?- nonvar(3.25).

true.

 

?- nonvar(loves(vincent,mia)).

true.

 

?- var(loves(_,mia)).

false.

 

?- nonvar(loves(_,mia)).

true.

 

?- X = a, var(X).

false.

 

?- X = a, nonvar(X).

X = a.

 

?-  var(X), X = a.

X = a.

 

?- nonvar(X), X = a.

false.

 

?- functor(f(a,b),F,A).

F = f,

A = 2.

 

?- functor([a,b,c],X,Y).

X = '.',

Y = 2.

 

// 注意两个不同的输出格式和差异

$ swipl 

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- functor([a,b,c],X,Y).

X = '[|]',

Y = 2.

 

?- halt.

 

swipl --traditional

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- functor([a,b,c],X,Y).

X = '.',

Y = 2.

 

?- functor(mia,F,A).

F = mia,

A = 0.

 

?- functor(8,F,A).

F = 8,

A = 0.

 

?- functor(3.25,F,A).

F = 3.25,

A = 0.

 

?- functor(T,f,7).

T = f(_6238, _6240, _6242, _6244, _6246, _6248, _6250).

 

?- [user].

|: complexterm(X):-

|: nonvar(X),

|: functor(X,_,A),

|: A > 0.

|: ^D% user://1 compiled 0.00 sec, 1 clauses

true.

 

?- arg(2,loves(vincent,mia),X).

X = mia.

 

?- arg(2,loves(vincent,X),mia).

X = mia.

 

?- arg(2,happy(yolanda),X).

false.

 

?- =..(loves(vincent,mia),X).

X = [loves, vincent, mia].

 

?- cause(vincent,dead(zed)) =.. X.

X = [cause, vincent, dead(zed)].

 

?- X =.. [a,b(c),d].

X = a(b(c), d).

 

?- footmassage(Y,mia) =.. X.

X = [footmassage, Y, mia].

 

?- S='vicky'.

S = vicky.

 

?- S='Vicky'.

S = 'Vicky'.

 

?- S="Vicky".

S = [86, 105, 99, 107, 121].

 

?- atom_codes(vicky,X).

X = [118, 105, 99, 107, 121].

 

?- atom_codes('Vicky',X).

X = [86, 105, 99, 107, 121].

 

?- atom_codes('Vicky Pollard',X).

X = [86, 105, 99, 107, 121, 32, 80, 111, 108|…].

 

?- atom_codes(abc,X), append(X,X,L), atom_codes(N,L).

X = [97, 98, 99],

L = [97, 98, 99, 97, 98, 99],

N = abcabc.

 

?- number_codes(0123456789,X).

X = [49, 50, 51, 52, 53, 54, 55, 56, 57].

 

?- is(11,+(2, *(3,3))).

true.

 

?- 2+3+4 = +(2,+(3,4)).

false.

 

?- 2+3+4 = +(+(2,3),4).

true.

 

?- [user].

|: :- op(500, xf, is_dead).

|: 

|: kill1(marsellui,zed).

|: is_dead(X) :- kill1(_,X).

|: ^D% user://1 compiled 0.00 sec, 2 clauses

true.

 

?- zed is_dead.

true.

 

练习

9.1 

?- zed is_dead.

true.

 

?- 12 is 2*6.

true.

 

?- 14 =\= 2*6.

true.

 

?- 14 = 2*7.

false.

 

?- 14 == 2*7.

false.

 

?- 14 \== 2*7.

true.

 

?- 14 =:= 2*7.

true.

 

?- [1,2,3|[d,e]] == [1,2,3,d,e].

true.

 

?- 2+3 == 3+2.

false.

 

?- 2+3 =:= 3+2.

true.

 

?- 7-2 =\= 9-2.

true.

 

?- p == 'p' .

true.

 

?- p =\= 'p'.

ERROR: Arithmetic: `p' is not a function

ERROR: In:

ERROR:   [20] throw(error(type_error(evaluable,p),_16764))

ERROR:   [17] arithmetic:expand_function(p,_16802,_16804) at /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/library/arithmetic.pl:175

ERROR:   [16] arithmetic:math_goal_expansion(p=\=p,_16836) at /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/library/arithmetic.pl:154

ERROR:   [14] '$expand':call_goal_expansion([system- ...],p=\=p,_16876,_16878,_16880) at /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/boot/expand.pl:876

ERROR:   [13] '$expand':expand_goal(p=\=p,_16930,_16932,_16934,user,[system- ...],_16940,[]) at /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/boot/expand.pl:525

ERROR:   [12] setup_call_catcher_cleanup('$expand':'$set_source_module'(user,user),'$expand':expand_goal(...,_17014,_17016,_17018,user,...,_17024,[]),_16988,'$expand':'$set_source_module'(user)) at /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/boot/init.pl:539

ERROR:    [8] '$expand':expand_goal(user:(p=\=p),_17068,user:_17090,_17072) at /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/boot/expand.pl:459

ERROR:    [6] setup_call_catcher_cleanup('$toplevel':'$set_source_module'(user,user),'$toplevel':expand_goal(...,...),_17118,'$toplevel':'$set_source_module'(user)) at /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/boot/init.pl:539

ERROR: 

ERROR: Note: some frames are missing due to last-call optimization.

ERROR: Re-run your program in debug mode (:- debug.) to get more detail.

^  Exception: (6) setup_call_catcher_cleanup('$toplevel':'$set_source_module'(user, user), '$toplevel':expand_goal(user:(p=\=p), _15556), _17176, '$toplevel':'$set_source_module'(user)) ? n

 

?- vincent == VAR.

false.

 

?- vincent=VAR, VAR==vincent.

VAR = vincent.

 

9.2

$ swipl --traditional

ERROR: /usr/local/Cellar/swi-prolog/HEAD-f5d97ff_1/libexec/lib/swipl/library/ansi_term.pl:428:13: Syntax error: Operator expected

Welcome to SWI-Prolog (threaded, 64 bits, version 8.1.15)

SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.

Please run ?- license. for legal details.

 

For online help and background, visit https://www.swi-prolog.org

For built-in help, use ?- help(Topic). or ?- apropos(Word).

 

?- .(a,.(b,.(c,[]))) = [a,b,c].

true.

 

?- .(a,.(b,.(c,[]))) = [a,b|[c]].

true.

 

?- .(.(a,[]),.(.(b,[]),.(.(c,[]),[]))) = X.

X = [[a], [b], [c]].

 

?- .(a,.(b,.(.(c,[]),[]))) = [a,b|[c]].

false.

 

9.3

?- [user].

|: termtype(Term,variable):-

|: var(Term).

|: termtype(Term,atom):-

|: atom(Term).

|: termtype(Term,number):-

|: number(Term).

|: termtype(Term,constant):-

|: atomic(Term).

|: termtype(Term,simple_term):-

|: atomic(Term).

|: termtype(Term,simple_term):-

|: var(Term).

|: termtype(Term,complex_term):-

|: nonvar(Term),

|: functor(Term,_,Arity),

|: Arity > 0.

|: termtype(Term,term):-

|: termtype(Term,simple_term).

|: termtype(Term,term):-

|: termtype(Term,complex_term).

|: ^D% user://1 compiled 0.00 sec, 9 clauses

true.

 

?- termtype(Vincent,variable).

true.

 

?- termtype(mia,X).

X = atom ;

X = constant ;

X = simple_term ;

X = term ;

false.

 

?- termtype(dead(zed),X).

X = complex_term ;

X = term.

 

9.4

?- [user].

|: groundterm(Term):-

|: atomic(Term).

|: groundterm(Term):-

|: nonvar(Term),

|: functor(Term,_,Arity),

|: groundterms(Term,Arity).

|: groundterms(_,0).

|: groundterms(ComplexTerm,Arg):-

|: Arg > 0,

|: arg(Arg,ComplexTerm,Term),

|: groundterm(Term),

|: NextArg is Arg - 1,

|: groundterms(ComplexTerm,NextArg).

|: ^D% user://2 compiled 0.00 sec, 4 clauses

true.

 

?-  groundterm(X).

false.

 

?- groundterm(french(bic_mac,le_bic_mac)).

true .

 

?- groundterm(french(whopper,X)).

false.

 

//使用单变量方案

?- [user].

|: groundterm(Term) :-

|: atomic(Term).

 

Warning: user://3:100:

Warning:    Redefined static procedure groundterm/1

Warning:    Previously defined at user://2:74

|: groundterm(Term) :-

|: nonvar(Term),

|: Term =.. [_|Args],

|: groundterms(Args).

|: groundterms([]).

|: groundterms([H|T]) :-

|: groundterm(H),

|: groundterms(T).

|: ^D% user://3 compiled 0.00 sec, 4 clauses

true.

 

?- groundterm(X).

false.

 

?- groundterm(french(bic_mac,le_bic_mac)).

true .

 

?- groundterm(french(whopper,X)).

false.

 

9.5

 

X is_a witch  is_a(X,witch)

harry and ron and hermione are friends are(and(harry,and(ron,hermione)),friends)

harry is_a wizard and likes quidditch 非Prolog项

dumbledore is_a famous wizard is_a(dumbledore,famous(wizard))

 

 

实践环节

//运行环境为:$ swipl —traditional

 

?- display(loves(vincent,mia)).

loves(vincent,mia)

true.

 

?- display('jules eats a big kahuna burger').

'jules eats a big kahuna burger'

true.

 

?- display(2+3+4).

+(+(2,3),4)

true.

 

?- display([a,b,c]).

'.'(a,'.'(b,'.'(c,[])))

true.

 

?- display(3 is 4 + 5 / 3).

is(3,+(4,/(5,3)))

true.

 

?- display(3 is (4 + 5) / 3).

is(3,/(+(4,5),3))

true.

 

?- display((a:-b,c,d)).

:-(a,','(b,','(c,d)))

true.

 

?- display(a:-b,c,d).

ERROR: Unknown procedure: display/3

ERROR:     However, there are definitions for:

ERROR:         edinburgh:display/1

false.

 

?- write(2+3+4).

2+3+4

true.

 

?- write(+(2,3)).

2+3

true.

 

?- write([a,b,c]).

[a,b,c]

true.

 

?- write(.(a,.(b,[]))).

[a,b]

true.

 

?- write(X).

_5964

true.

 

?- X = a, write(X).

a

X = a.

 

?- write(a),write(b).

ab

true.

 

?- write(a),write(' '),write(b).

a b

true.

 

?- write(a),write('    '),write(b).

a    b

true.

 

?- write(a),tab(5),write(b).

a     b

true.

 

?- write(a),nl,write(b).

a

b

true.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值