Request: (count) ... (into $Var)

It is useful for me to sometime know the number of items that match a predicate. I think the general form might be:

(count)
  *(some-trait $Foo)
  (frob $Foo)
(into $FooCount)

I don’t need the list, just the length of the list if I did form it.

Also, I can’t seem to find a built-in for the length of a list.

2 Likes

I second that request.

But as for the length:

(length of $List into $N)
1 Like

I’ve been thinking about adding something like this:

(accumulate $Term)
        ...code that binds $Term to a number...
(into $Sum)

It could be useful for computing the total weight of an object and its recursive descendants, for instance.

Counting the elements of a list would then be a special case:

(accumulate 1)
        *(some-trait $Foo)
        (frob $Foo)
(into $FooCount)
1 Like

This seems to be working for me. What do you think?

%% getting the number of items in a list that satisfy a predicate (closure)
%% we need to use the $_ variable inside the closure
%% to pass the items in the list to be tested in the predicate
%% for example: 
%% (number of {($_ < 5)} in $List into $N)

(number of $Closure in $List into $N)
	(collect $Element){
		*($Element is one of $List)
		(query $Closure $Element)}
	(into $Sublist)
	(length of $Sublist into $N)
1 Like

Yes, that seems like a sensible way to do it. The accumulate-feature would be faster, but it’s not in the language (yet).

1 Like

That would be very nice. :grinning:

a thing to remember, dialog is prolog-like, but isn’t prolog. I’m correct, Ake ?

Best regards from Italy,
dott. Piergiorgio.

That is correct.

The main similarities with Prolog are:

  • Unification
  • Backtracking
  • Predicate logic
  • Lists with structural recursion
  • A mutable “database” of dynamic predicates

The main differences are:

  • In Dialog, predicates are not “first class”, meaning that they can’t be passed around as values. Prolog has terms to represent computations as data.
  • The concrete syntax is different. Dialog has a markup-like syntax, where the primary type of content is text. Prolog is more like a traditional programming language, where the primary type of content is code.
  • Many Prolog versions include a Finite Domain constraint-solver, but Dialog does not.
  • Dialog has special IF-related features, like object tree operations, status bar, save/restore/undo, and so on.

What’s funny is I could not find that in the doc index. Who snuck onto my computer last night and patched the file?

I don’t know, but I think s/he lives in a secret room in my house and doesn’t just mess with my computer.

The syntax we discussed above, (accumulate $) ... (into $), has been introduced in Version 0k.

1 Like