root/trunk/examples/metapredicates/SCRIPT.txt

Revision 4668, 3.0 KB (checked in by pmoura, 4 days ago)

Corrected some minor typos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1================================================================
2Logtalk - Open source object-oriented logic programming language
3Release 2.35.0
4
5Copyright (c) 1998-2009 Paulo Moura.        All Rights Reserved.
6Logtalk is free software.  You can redistribute it and/or modify
7it under the terms of the "Artistic License 2.0" as published by
8The Perl Foundation. Consult the "LICENSE.txt" file for details.
9================================================================
10
11
12% start by loading the example and the required library files:
13
14| ?- logtalk_load(metapredicates(loader)).
15...
16
17
18% note that "user" is a pseudo-object representing the Prolog database;
19% therefore, the integer comparisons are performed using the standard
20% Prolog built-in predicates
21
22| ?- sort(user)::sort([3, 1, 4, 2, 9], Sorted).
23
24call: partition([1,4,2,9],3,_358,_359)
25exit: partition([1,4,2,9],3,[1,2],[4,9])
26call: sort([1,2],_740)
27call: partition([2],1,_967,_968)
28exit: partition([2],1,[],[2])
29call: sort([],_1300)
30exit: sort([],[])
31call: sort([2],_1539)
32call: partition([],2,_1765,_1766)
33exit: partition([],2,[],[])
34call: sort([],_2093)
35exit: sort([],[])
36call: sort([],_2332)
37exit: sort([],[])
38exit: sort([2],[2])
39exit: sort([1,2],[1,2])
40call: sort([4,9],_2831)
41call: partition([9],4,_3058,_3059)
42exit: partition([9],4,[],[9])
43call: sort([],_3391)
44exit: sort([],[])
45call: sort([9],_3630)
46call: partition([],9,_3856,_3857)
47exit: partition([],9,[],[])
48call: sort([],_4184)
49exit: sort([],[])
50call: sort([],_4423)
51exit: sort([],[])
52exit: sort([9],[9])
53exit: sort([4,9],[4,9])
54
55Sorted = [1,2,3,4,9]
56yes
57
58
59% call the meta-predicate apply/2 directly:
60
61| ?- metapreds::test_this.
62
631, metapreds
64yes
65
66
67% send an apply/2 message to self:
68
69| ?- descendant::test_self.
70
712, descendant
72yes
73
74
75% send an apply/2 message from another object:
76
77| ?- test::test_obj.
78
793, test
80yes
81
82
83% use the partition/4 predicate with a predicate defined in the
84% pseudo-object "user":
85
86| ?- meta::partition(even_integer, [1,2,3,4,5], Included, Excluded).
87
88Included = [2, 4]
89Excluded = [1, 3, 5]
90yes
91
92
93% use the fold_left/4 predicate with a predicate defined in the
94% pseudo-object "user":
95
96| ?- meta::fold_left(sum_squares, 0, [1,2,3], Result).
97
98Result = 34
99yes
100
101
102% use the fold_left/4 and fold_right/4 predicates with the atom_concat/3
103% Prolog built-in predicate:
104
105| ?- meta::fold_left(atom_concat, 'PREFIX', [abc,def,ghi], Result).
106
107Result = 'PREFIXabcdefghi'
108yes
109
110| ?- meta::fold_right(atom_concat, 'SUFIX', [abc,def,ghi], Result).
111
112Result = abcdefghiSUFIX
113yes
114
115
116% use the fold_left/4 predicate with predicates defined in the object
117% "predicates":
118
119| ?- meta::fold_left(predicates::sum, 0, [1,2,3,4,5], Result).
120
121Result = 15
122yes
123
124| ?- meta::fold_left(predicates::product, 1, [1,2,3,4,5], Result).
125
126Result = 120
127yes
128
129| ?- meta::fold_left(predicates::tuple, (0,0), [(1,2), (3,4), (6,4)], Result).
130
131Result = (10, 10)
132yes
133
134
135% use the map/2-3 predicates with some Prolog built-in predicates:
136
137| ?- meta::map(integer, [1,2,3,4,5]).
138
139yes
140
141| ?- meta::map(char_code, [a,b,c,d,e], Codes).
142
143Codes = [97, 98, 99, 100, 101]
144yes
Note: See TracBrowser for help on using the browser.