Parameter Passing to Logtalk predicates

Help requests on developing Logtalk applications

Moderator: Paulo Moura

Post Reply
Monikuta
Posts: 13
Joined: Mon May 12, 2008 9:02 am

Parameter Passing to Logtalk predicates

Post by Monikuta » Tue May 20, 2008 6:38 am

Hello,
Suppose I want to create inside my object "main" some random objects of type "student" and add them to a list.
How do I pass an object of type "student" as a parameter to a predicate defined inside the object "main" ?
I tried using "student(XI,XN,XM)" as a parameter to the predicates "member", "add", "randomStudent",
where XI = Id, XN = Name, XM = Mean, but this doesn't' work.

Code: Select all

:- object(main).
  ....
   member(student(XI, XN, XM), [student(XI, XN, XM)|_]).
   member(student(XI, XN, XM), [_|Tail]):-member(student(XI, XN, XM),Tail).

   add(student(XI, XN, XM),L1,L1) :- member(student(XI, XN, XM),L1), !.
   add(student(XI, XN, XM),L1, [student(XI, XN, XM)|L1]).


   randomStudent(student(SI, SN, SM)) :- randomId(SI),
                                         randomName(SN),
                                         randomMean(SM).

   randomId(Id) :- Id is irand(50).

   randomName(Name) :- I is irand(50),
                       Str=to_string(I),
                       string_concat('name',Str,Name).

   randomMean(Mean) :- M is irand(1000),
                       Mean is M/100.

   randomList(Stud, 0):- write('Students list: '),
                         nl,
                         writelist(Stud).
   randomList(Stud, N) :- N > 0,
                          student::new(S),
                          randomStudent(S),
                          add(S, Stud, Stud1),
                          N1 is N-1,
                          randomList(Stud1, N1).
  ....
:- end_object.
I look forward to receiving your response. Thanks in advance.
Yours sincerely,
Monica Dogaru

Paulo Moura
Logtalk developer
Posts: 475
Joined: Sat May 05, 2007 8:35 am
Location: Portugal
Contact:

Re: Parameter Passing to Logtalk predicates

Post by Paulo Moura » Tue May 20, 2008 8:05 am

Monikuta wrote:Hello,
Suppose I want to create inside my object "main" some random objects of type "student" and add them to a list.
In that case, your list would store the *identifiers* of student instance you've created.
Monikuta wrote: How do I pass an object of type "student" as a parameter to a predicate defined inside the object "main" ?
Simply call the "main" predicate using the object identifier.
Monikuta wrote: I tried using "student(XI,XN,XM)" as a parameter to the predicates "member", "add", "randomStudent",
where XI = Id, XN = Name, XM = Mean, but this doesn't' work.
....
Assuming that you're using the implementation of the new/1 predicate found on the Logtalk "roots" example, this predicate returns the identifier of the new object. This identifier will not unify with the compound term "student(XI,XN,XM)", which will lead to failure of your randomList/2 predicate call. It would be much simpler to use the new/2 predicate to create a "student" instance with initialization options for the student name and mean (see the "bricks" example). Nevertheless, keep in mind that Logtalk is an object-oriented logic-programming language. Doing in Logtalk the kind of programming found e.g. in Java is often not the best solution (specially in terms of performance). Do you really need to dynamically create (and abolish) objects at runtime? Note that static objects may have dynamic predicates if you need to implement some kind of mutable state. In addition, parametric objects provides you with an implementation solution for objects whose state is set at object creation time and never changes during the lifetime of the object.

Best regards,

Paulo
Paulo Moura
Logtalk developer

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest