Finding maximum or minimum values

As I play with an automapper for Dialog, I need to figure out the overall size of the map, to draw it appropriately. Assigning coordinates to the rooms is straightforward, but I’m realizing I don’t actually know how to find the minimum value of something.

For example, perhaps I have a predicate like (room $Room is located at $X $Y $Z), and I want to find the minimum X-value on the first floor (Z=1). I can iterate over all rooms with Z=1, and I can gather all their X-values with a (collect $) predicate, but there doesn’t seem to be any straightforward way to find the minimum of them.

Is the best solution to collect them all into a list, then write my own (minimum of $List into $Res) predicate?

Naïve implementation:

(minimum of [$Head|$List] is $Min)
	(minimum of $List or $Head is $Min)
(minimum of [] or $Min is $Min)
(minimum of [$Head|$List] or $Prev is $Min)
	($Head < $Prev)
	(minimum of $List or $Head is $Min)
(minimum of [$Head|$List] or $Prev is $Min)
	(minimum of $List or $Prev is $Min)

I don’t know how performant this would be, though.

Would this work?

(minimum [$H] into $M)    ($M = $H)
(minimum [$H|$L] into $M) (minimum $L into $K)
                          (if) ($H < $K)
                          (then) ($M = $H)
                          (else) ($M = $K)
                          (endif)
2 Likes