This may be a completely off the wall idea, but after @peter.ludemann introduced me to foldl/4
yesterday, I’ve been playing, and am impressed with its power.
At the same time, I was toying with an exercise in a book that required you to write a predicate ordered(L)
which is true if the elements of L are in numerical order.
I came up with a solution (not posted here as it’s not relevant), but wondered if I could have done it as a one-liner using foldl/4
. My idea was something like…
ordered([H|T]) :-
foldl(=<, T, H, true).
Now this doesn’t compile for a couple of reasons, but hopefully you can see where I was trying to go with this. I tried defining a predicate to use instead of =<
but couldn’t get that to work either.
Is it possible to do something like this, or am I being totally off-field here?
Thanks