Compound terms and lists in Prolog

Why are complex terms preferable to lists in terms of performance? For instance,

matrix(row(1,2,3),row(1,2,3),row(1,2,3))

preferable

[[1,2,3],[1,2,3],[1,2,3],[1,2,3]]

Thank!

+4
source share
3 answers

Something that the other (excellent) answer did not mention:

Access to the list item by its position means that you need to go through the list. Access to the term argument should be possible at a constant time. Thus, for random access, the term should be more effective.


Put aside: you can try to make the list move slightly faster. But the SWI-Prolog implementationnth0_det/3 almost smells like despair;)


, . , .

.

:

  • , , .
  • 0 , .
  • , .

:

  • ,
  • , Prolog - .

: , , . , . - : Prolog .

, , , . , , .

, , , . , , " " ++, , ( " O" ) , .

+5

-, : .

, , write_canonical/1. , GNU Prolog:

| ?- write_canonical([1,2,3]).  
'.'(1,'.'(2,'.'(3,[])))

, '. , , row(1,2,3) :

  • /arity
  • 3

.(1, .(2, .(3, []) :

  • 3 '.'/2
  • 3 1, 2, 3
  • (, []).

, .

, , .

+6

. . functor(arg1, arg2, ..., argn, ...), arg/3 . O (N) O (1) Prolog.

, . , . , ( arg/3). Prolog. , . , , .

+3

All Articles