How does the length of list predicate work - list

I know there are some topics already about this. But I still dont get it.
Could be somebody describe step by step how:
length1([],0).
length1([X|Xs],N) :-length1(Xs,M), N is M+1.
works? I put traces on but I still have no idea.
Thanks!

Let's walk through an example:
?- length1([a, b, c], N).
First Prolog tries to unify with the first rule, but [a, b, c] doesn't match []. So it tries to unify with the second rule, and we get:
length1([a|[b, c]], N) :-
length1([b, c], M), % We do this first
N is M + 1. % We can't solve this yet because we don't know what M is
So we've matched [a, b, c] to [a|[b,c]], which is part of the list syntax. We can't work out what N is yet, but we've got to do the middle line first, which unifies with the same rule:
length1([b|[c]], M) :-
length1([c], L),
M is L + 1.
As before, we need to do the middle line to work out what M is.
length1([c|[]], L) :-
length1([], K),
L is K + 1.
We still can't work it out, but now our middle line unifies with the first rule:
length1([], 0). % So now we know what K is!
We've unified K with 0, so now we can start going back up:
length1([c|[]], 1) :-
length1([], 0),
1 is 0 + 1. % L is K + 1
length1([b|[c]], 2) :-
length1([c], 1),
2 is 1 + 1. % M is L + 1
length1([a|[b, c]], 3) :-
length1([b, c], 2),
3 is 2 + 1. % N is M + 1
So we exit our recursion with N unified to 3:
?- length1([a, b, c], N).
N = 3.

List-processing predicates in Prolog are almost always going to have two clauses, because the list type is defined inductively as:
A list is either:
Empty: [], or
An item, plus a list: [X|Xs]
Inductive data structures often have predicates whose definitions break down into the same cases as the structure itself. In the case of length, the inductive definition looks like:
The length of a list is either:
0 for empty lists, or
1 + the length of the rest of the list
And that's exactly what you have here in the Prolog code:
length1([], 0).
says that the length of the empty list is zero. This is your base case. A better second clause would be:
length1([_|Xs], N1) :- length1(Xs, N), N1 is N+1
This says, supposing you have a list with some item (we don't care about what it is) and a tail of a list, the length of this list is N1, where N1 is 1 + the length of the tail of the list.

It works badly. Here is how it is supposed to work:
?- length([], N).
N = 0.
?- length([_,_], N).
N = 2.
?- length(L, 0).
L = [].
?- length(L, 2).
L = [_5494, _5500].
?- length(L, N).
L = [],
N = 0 ;
L = [_5524],
N = 1 ;
L = [_5524, _5530],
N = 2 .
?- L = [_|L], length(L, N).
ERROR: Type error: `list' expected, found `#(S_1,[S_1=[_6396|S_1]])' (a cyclic)
ERROR: In:
ERROR: [12] throw(error(type_error(list,...),context(...,_6460)))
ERROR: [9] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
... and here is how the version from your question works:
?- length1([], N).
N = 0. % OK
?- length1([_,_], N).
N = 2. % OK
?- length1(L, 0).
L = [] ; % does not terminate!
^CAction (h for help) ? abort
% Execution Aborted
?- length1(L, 2).
L = [_9566, _9572] ; % does not terminate
^CAction (h for help) ? abort
% Execution Aborted
?- length1(L, N).
L = [],
N = 0 ;
L = [_9602],
N = 1 ;
L = [_9602, _9608],
N = 2 . % OK
?- L = [_|L], length1(L, N).
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 1.0Gb, global: 37Kb, trail: 1Kb
ERROR: Stack depth: 11,180,538, last-call: 0%, Choice points: 3
ERROR: Probable infinite recursion (cycle):
ERROR: [11,180,538] user:length1([cyclic list], _9792)
ERROR: [11,180,537] user:length1([cyclic list], _9818)
The last one looks the same, but the built-in length/2 throws a type error instead of exceeding the stack.
There is another problem with length1/2, it uses up the stack if the list is too long, because every recursive step must be left on the stack with the current definition.
?- length(L, 10 000 000). % make a long list, works
L = [_984, _990, _996, _1002, _1008, _1014, _1020, _1026, _1032|...].
?- length(L, 10 000 000), length(L, N). % how long is the long list?
L = [_1258, _1264, _1270, _1276, _1282, _1288, _1294, _1300, _1306|...],
N = 10000000.
?- length(L, 10 000 000), length1(L, N). % how long is the list? too long!
ERROR: Stack limit (1.0Gb) exceeded
ERROR: Stack sizes: local: 0.5Gb, global: 0.2Gb, trail: 1Kb
ERROR: Stack depth: 5,592,186, last-call: 0%, Choice points: 3
ERROR: Possible non-terminating recursion:
ERROR: [5,592,186] user:length1([length:4,407,825], _60000944)
ERROR: [5,592,185] user:length1([length:4,407,826], _60000970)
Moreover:
?- time( length(L, 1 000) ).
% 2 inferences, 0.000 CPU in 0.000 seconds (86% CPU, 46417 Lips)
L = [_886, _892, _898, _904, _910, _916, _922, _928, _934|...].
?- time( length(L, 10 000) ).
% 2 inferences, 0.001 CPU in 0.001 seconds (99% CPU, 3495 Lips)
L = [_888, _894, _900, _906, _912, _918, _924, _930, _936|...].
?- time( length1(L, 1 000) ).
% 501,499 inferences, 0.067 CPU in 0.067 seconds (100% CPU, 7511131 Lips)
L = [_878, _884, _890, _896, _902, _908, _914, _920, _926|...] .
?- time( length1(L, 10 000) ).
% 50,014,999 inferences, 4.938 CPU in 4.941 seconds (100% CPU, 10128365 Lips)
L = [_876, _882, _888, _894, _900, _906, _912, _918, _924|...] .
It takes too long to make bigger lists using length1/2 so this is why I am showing it with such a short list. But you might notice that a list 10 times longer makes your length1/2 run 100 times longer. You should ask whoever showed you this code what that means.
Why am I writing this? Not sure. I think it is irresponsible to show students shitty code. On the other hand, this is a popular question (in the sense that many people before you have asked the exact same thing).

Related

Why does Prolog skip all further if statements in recursion if one of them fails?

The point of the program is to calculate the sum of all even numbers in a list of integers.
is_even(Q):- Q mod 2 =:= 0.
sum_even([],0).
sum_even([A|L],X) :- sum_even(L,X1) -> is_even(A) -> X is X1 + A .
Whenever the is_even predicate succeeds there is no problem, it normally goes back to calculate the sum. However when the number is not even and is_even checks it, it fails, goes back to the recursion and fails everything that follows, doesn't even check if the number is even anymore and just returns false. In a list full of even numbers it works as intended, it returns the sum of all numbers in the list. This here is the trace of the code
Using an accumulator with tail-end recursion is fastest:
is_even(N) :-
N mod 2 =:= 0.
sum_even(Lst, Sum) :-
sum_even_(Lst, 0, Sum).
sum_even_([], Sum, Sum).
sum_even_([H|T], Upto, Sum) :-
( is_even(H) ->
Upto1 is Upto + H
; Upto1 = Upto
),
sum_even_(T, Upto1, Sum).
sum_even_slow([], 0).
sum_even_slow([H|T], Sum) :-
sum_even_slow(T, Sum0),
( is_even(H) ->
Sum is Sum0 + H
; Sum = Sum0
).
Performance comparison in swi-prolog:
?- numlist(1, 1_000_000, L), time(sum_even(L, Sum)).
% 4,000,002 inferences, 0.765 CPU in 0.759 seconds (101% CPU, 5228211 Lips)
Sum = 250000500000.
?- numlist(1, 1_000_000, L), time(sum_even_slow(L, Sum)).
% 4,000,001 inferences, 4.062 CPU in 4.023 seconds (101% CPU, 984755 Lips)
Sum = 250000500000.
add_even(X, Buffer, Sum) :-
(0 =:= X mod 2 ->
Sum is Buffer + X
; Sum = Buffer).
Used with foldl(add_even, Nums, 0, Sum) is about as fast as brebs' tail recursion. Using SWISH Online:
?- numlist(1, 1_000_000, _L), time(sum_even(_L, Sum)).
4,000,002 inferences, 0.699 CPU in 0.699 seconds (100% CPU, 5724382 Lips)
Sum = 250000500000
?- numlist(1, 1_000_000, _L), time(foldl(add_even, _L, 0, Sum)).
4,000,002 inferences, 0.712 CPU in 0.712 seconds (100% CPU, 5621314 Lips)
Sum = 250000500000
(I am curious, if they both do exactly 4,000,002 inferences how come the sum_even seems to get higher LIPS throughput and finish slightly faster?)
The reason that this
is_even(Q):- Q mod 2 =:= 0.
sum_even([],0).
sum_even([A|L],X) :- sum_even(L,X1) -> is_even(A) -> X is X1 + A .
fails when there's odd numbers in the list is that '->'/2, the "implies" operator, is a soft cut. It eliminates the choice point. That means that backtracking into it fails, thus unwinding the whole thing and failing the entire predicate.
Your code is roughly equivalent to
sum_even([],0).
sum_even([A|L],X) :- sum_even(L,X1), !, is_even(A), !, X is X1 + A .
You need to
Eliminate the '->'/2 — it's not necessary the ',' operator is the AND operator, and
Provide an alternative to handle the case when an item is odd.
For instance, you could say this:
is_even( Q ) :- Q mod 2 =:= 0.
sum_even( [] , 0 ) .
sum_even( [A|L] , X ) :-
sum_even(L,X1),
( is_even(A) ->
X is X1 + A
;
X is X1
) .
Or something like this:
is_even( Q ) :- Q mod 2 =:= 0.
sum_even( [] , 0 ) .
sum_even( [A|L] , X ) :-
sum_even(L,X1),
add_even(A,X1,X) .
add_even(X,Y,Z) :- is_even(X), !, Z is X + Y .
add_even(_,Z,Z) .
But the easiest, faster way is to use a helper predicate that carries the extra state. The above predicates will die with a stack overflow given a sufficiently long list. This will not: Prolog has TRO (tail recursion optimization) built in, which effectively turns the recursive call into iteration by reusing the stack frame, so it can handle recursion of any depth:
sum_even( Xs , S ) :- sum_even(Xs,0,S) .
sum_even( [] , S , S ) .
sum_even( [X|Xs] , T , S ) :- is_even(X), !, T1 is T+X, sum_even(Xs,T1,S) .
is_even( Q ) :- Q mod 2 =:= 0.

Give as a solution every different number in a list of lists

I need to do a predicate, select(ListOfLists, X) that returns as a solution every different number in a list of lists, starting with the numbers that are alone in a list, for example:
select([[1,2,3],[1,2],[4],[3]],X).
Would return:
X = 4 ;
X = 3 ;
X = 2 ;
X = 1
Order doesn't matter as long as the numbers that are alone in the list are shown first.
To do this, first I coded 2 other predicates, which are:
%OrderedList is Lists ordered by size.
orderListsBySize(Lists, OrderedLists).
Example: orderListsBySize([[1,2],[6],[3,4,5]], L). ->L = [[6], [1,2], [3,4,5]]
And
%ListsWithoutX is Lists without the X elements
removeFromLists(X, Lists, ListsWithoutX).
Example: removeFromLists(1,[[1,2],[3],[4,1,5]],L). -> L = [[2],[3],[4,5]]
Both predicates work.
Then, to do the select(ListOfLists, X) predicate, I tried the following:
select([[X|[]]|_], X). select(L1,S) :-
orderListsBySize(L1, [[X|XS]|LS]),
length(XS, A),
A == 0,
select([[X|[]]|M], S),
removeFromLists(X, [XS|LS], M).
select([[X|_]|_], X).
But it doesn't work.
It's not a hard exercise to do in other languages, the problem is that it's still hard for me to understand how prolog works. I appreaciate any help, thanks!
You could start with:
select2(ListOfLists,Element):-
length(List,_Len),
member(List,ListOfLists),
member(Element,List).
Which will return all the answers, but then get stuck in a loop looking for ever bigger lists.
This can be averted using the :-use_module(library(clpfd)). and defining a fd_length/2 which wont keep looking for bigger lists then exist in the list of lists.
fd_length(L, N) :-
N #>= 0,
fd_length(L, N, 0).
fd_length([], N, N0) :-
N #= N0.
fd_length([_|L], N, N0) :-
N1 is N0+1,
N #>= N1,
fd_length(L, N, N1).
select(ListOfLists,Element):-
maplist(length,ListOfLists,Lengths),
sort(Lengths,SortedLength),
last(SortedLength,Biggest),
Biggest #>= Len,
fd_length(List,Len),
member(List,ListOfLists),
member(Element,List).
Example Query:
?-select([[1,2,3],[1,2],[4],[3]],X).
X = 4
X = 3
X = 1
X = 2
X = 1
X = 2
X = 3
false
If you want unique solutions, you could enclose in a setof/3 and then call member/2 again.

Arguments are not sufficiently instantiated in relation with 2 examples

That's a very frequently asked question but i'd like to pose it in relation with two examples that seem very similar in my eyes, and are yet the one correct and the other not.
Correct example:
k_th_element(X,[X|_],1).
k_th_element(X,[_|L],K):- K>1,K1 is (K-1),k_th_element(X,L,K1).
Wrong Example
length2(1,[_]).
length2(X,[_|Ys]) :- X>1, X1 is (X-1), length(X1,Ys).
Why prolog complains or doesn't for each case?
Update: I think i got it. What i couldn't understand was that it doesn't matter what the predicate is but how you are calling it. so this is correct:
k_th_element(X,[1,2,3,4,5],3) because you have a value for K which is the right variable of "is" operator. But at the same time k_th_element(3,[1,2,3,4,5],Y) will not work, because Y is a variable, our "goal" and we can't have that in the right part of "is" operator. Correct me if i'm wrong.
as mat proposed, there is a more flexible way to achieve the same:
:- use_module(library(clpfd)).
length2(0,[]).
length2(X,[_|Ys]) :- X#>0, X1#=X-1, length2(X1,Ys).
First, there is the argument order. For length/2 it is rather length(List, Length).
For the case of a given list and an unknown length, your version is relatively inefficient because of all the X1 #= X-1 constraints which implies about N constrained variables. The version length3/2 has a single constrained variable. (It is about 7 times faster. I am still surprised that it is not faster than it is, maybe someone can help with another answer?)
:- use_module(library(clpfd)).
length2([], 0).
length2([_E|Es], N0) :-
N0 #> 0,
N1 #= N0-1,
length2(Es, N1).
length3(Es, N) :-
length3(Es, 0, N).
length3([], N,N).
length3([_E|Es], N0,N) :-
N1 is N0+1,
N #>= N1,
length3(Es, N1,N).
?- length(L,1000), time(length2(L,N)).
% 783,606 inferences, 0.336 CPU in 0.347 seconds (97% CPU, 2332281 Lips)
L = [_A, _B, _C, _D, _E, _F, _G, _H, _I|...], N = 1000.
?- length(L,1000), time(length3(L,N)).
% 127,006 inferences, 0.047 CPU in 0.058 seconds (81% CPU, 2719603 Lips)
L = [_A, _B, _C, _D, _E, _F, _G, _H, _I|...], N = 1000.
Using reflection predicates one can build the following variant of list_length/2:
:- use_module(library(clpfd)).
list_length(Es, N) :-
( fd_sup(N, Ub), integer(Ub)
-> ( length(Es, M),
( M >= Ub
-> !,
M == Ub
; true
),
M = N
)
; length(Es, N)
).
The above implementation combines two nice properties:
It works well with clpfd.
In particular, list_length(Xs,N) terminates universally whenever N has a finite upper bound.
Using SWI-Prolog 8.0.0:
?- N in 1..3, list_length(Xs, N).
N = 1, Xs = [_A]
; N = 2, Xs = [_A,_B]
; N = 3, Xs = [_A,_B,_C]. % terminates universally
It minimizes auxiliary computation (and thus runtime) by using the builtin predicate length/2.
Let's compare the runtime of list_length/2 with length3/2—presented in this earlier answer!
Using SWI-Prolog 8.0.0 with command-line option -O:
?- time(( N in 1..100000, list_length(_,N), false ; true )).
% 2,700,130 inferences, 0.561 CPU in 0.561 seconds (100% CPU, 4812660 Lips)
true.
?- time(( N in 1..100000, length3(_,N), false ; true )).
% 14,700,041 inferences, 3.948 CPU in 3.949 seconds (100% CPU, 3723234 Lips)
true.
Note that the above also works with SICStus Prolog: SWI's fd_sup/2 is called fd_max/2 in SICStus.

Building a list of n length same value Prolog

I want to build/3 a list in Prolog of N elements, each element should be X.
?- build(30,3,L).
L = [30,30,30].
Spent a good few hours on it, keep ending up in either a infinite loop or the variables aren't instantiated properly.
build(_,0,[]).
build(X,N1,[X]):- N1>0, N1 is N - 1, build(X,N,[]).
build(X,N1,[X|L]) :- N1 > 0, N1 is N - 1, build(X,N,L).
Using meta-predicates makes it very short:
(1) with maplist/2: creates a list of length N and then matches all its elements to X.
build(X, N, List) :-
length(List, N),
maplist(=(X), List).
(2) with findall/3: makes loop N-times and completes List with X N-times
build(X, N, List) :-
findall(X, between(1, N, _), List).
build(_,0,[]). % any value, repeated 0 times, makes for an empty list
good.
build(X,N1,[X|L]) :- % a value X, repeated N1 times, makes for [X|L] list, _if_ ...
N1 > 0, N1 is N - 1, % N1 is positive, and L is
build(X,N,L). % one element shorter... right?
excellent. huh? N is N1 - 1 you mean.
build(X,N1,[X]):- N1>0, N1 is N - 1, build(X,N,[]).
why?? [X] is already matched by the previous rule, [X] = [X | [] ] = [X | L], and the empty list L = [] will be matched by the first rule.
You don't need this rule at all.

Prolog, find minimum in a list

in short: How to find min value in a list? (thanks for the advise kaarel)
long story:
I have created a weighted graph in amzi prolog and given 2 nodes, I am able to retrieve a list of paths. However, I need to find the minimum value in this path but am unable to traverse the list to do this. May I please seek your advise on how to determine the minimum value in the list?
my code currently looks like this:
arc(1,2).
arc(2,3).
arc(3,4).
arc(3,5).
arc(3,6).
arc(2,5).
arc(5,6).
arc(2,6).
path(X,Z,A) :-
(arc(X,Y),path(Y,Z,A1),A is A1+1;arc(X,Z), A is 1).
thus, ' keying findall(Z,path(2,6,Z),L).' in listener allows me to attain a list [3,2,2,1].
I need to retrieve the minimum value from here and multiply it with an amount. Can someone please advise on how to retrieve the minimum value? thanks!
It is common to use a so-called "lagged argument" to benefit from first-argument indexing:
list_min([L|Ls], Min) :-
list_min(Ls, L, Min).
list_min([], Min, Min).
list_min([L|Ls], Min0, Min) :-
Min1 is min(L, Min0),
list_min(Ls, Min1, Min).
This pattern is called a fold (from the left), and foldl/4, which is available in recent SWI versions, lets you write this as:
list_min([L|Ls], Min) :- foldl(num_num_min, Ls, L, Min).
num_num_min(X, Y, Min) :- Min is min(X, Y).
Notice though that this cannot be used in all directions, for example:
?- list_min([A,B], 5).
is/2: Arguments are not sufficiently instantiated
If you are reasoning about integers, as seems to be the case in your example, I therefore recommend you use CLP(FD) constraints to naturally generalize the predicate. Instead of (is)/2, simply use (#=)/2 and benefit from a more declarative solution:
:- use_module(library(clpfd)).
list_min([L|Ls], Min) :- foldl(num_num_min, Ls, L, Min).
num_num_min(X, Y, Min) :- Min #= min(X, Y).
This can be used as a true relation which works in all directions, for example:
?- list_min([A,B], 5).
yielding:
A in 5..sup,
5#=min(B, A),
B in 5..sup.
This looks right to me (from here).
min_in_list([Min],Min). % We've found the minimum
min_in_list([H,K|T],M) :-
H =< K, % H is less than or equal to K
min_in_list([H|T],M). % so use H
min_in_list([H,K|T],M) :-
H > K, % H is greater than K
min_in_list([K|T],M). % so use K
%Usage: minl(List, Minimum).
minl([Only], Only).
minl([Head|Tail], Minimum) :-
minl(Tail, TailMin),
Minimum is min(Head, TailMin).
The second rule does the recursion, in english "get the smallest value in the tail, and set Minimum to the smaller of that and the head". The first rule is the base case, "the minimum value of a list of one, is the only value in the list".
Test:
| ?- minl([2,4,1],1).
true ?
yes
| ?- minl([2,4,1],X).
X = 1 ?
yes
You can use it to check a value in the first case, or you can have prolog compute the value in the second case.
This program may be slow, but I like to write obviously correct code when I can.
smallest(List,Min) :-
sort(List,[Min|_]).
SWI-Prolog offers library(aggregate). Generalized and performance wise.
:- [library(aggregate)].
min(L, M) :- aggregate(min(E), member(E, L), M).
edit
A recent addition was library(solution_sequences). Now we can write
min(L,M) :- order_by([asc(M)], member(M,L)), !.
max(L,M) :- order_by([desc(M)], member(M,L)), !.
Now, ready for a surprise :) ?
?- test_performance([clpfd_max,slow_max,member_max,rel_max,agg_max]).
clpfd_max:99999996
% 1,500,000 inferences, 0.607 CPU in 0.607 seconds (100% CPU, 2470519 Lips)
slow_max:99999996
% 9,500,376 inferences, 2.564 CPU in 2.564 seconds (100% CPU, 3705655 Lips)
member_max:99999996
% 1,500,009 inferences, 1.004 CPU in 1.004 seconds (100% CPU, 1494329 Lips)
rel_max:99999996
% 1,000,054 inferences, 2.649 CPU in 2.648 seconds (100% CPU, 377588 Lips)
agg_max:99999996
% 2,500,028 inferences, 1.461 CPU in 1.462 seconds (100% CPU, 1710732 Lips)
true
with these definitions:
```erlang
:- use_module(library(clpfd)).
clpfd_max([L|Ls], Max) :- foldl([X,Y,M]>>(M #= max(X, Y)), Ls, L, Max).
slow_max(L, Max) :-
select(Max, L, Rest), \+ (member(E, Rest), E #> Max).
member_max([H|T],M) :-
member_max(T,N), ( \+ H#<N -> M=H ; M=N ).
member_max([M],M).
rel_max(L,M) :-
order_by([desc(M)], member(M,L)), !.
agg_max(L,M) :-
aggregate(max(E), member(E,L), M).
test_performance(Ps) :-
test_performance(Ps,500 000,_).
test_performance(Ps,N_Ints,Result) :-
list_of_random(N_Ints,1,100 000 000,Seq),
maplist({Seq}/[P,N]>>time((call(P,Seq,N),write(P:N))),Ps,Ns),
assertion(sort(Ns,[Result])).
list_of_random(N_Ints,L,U,RandomInts) :-
length(RandomInts,N_Ints),
maplist({L,U}/[Int]>>random_between(L,U,Int),RandomInts).
clpfd_max wins hands down, and to my surprise, slow_max/2 turns out to be not too bad...
SWI-Prolog has min_list/2:
min_list(+List, -Min)
True if Min is the smallest number in List.
Its definition is in library/lists.pl
min_list([H|T], Min) :-
min_list(T, H, Min).
min_list([], Min, Min).
min_list([H|T], Min0, Min) :-
Min1 is min(H, Min0),
min_list(T, Min1, Min).
This is ok for me :
minimumList([X], X). %(The minimum is the only element in the list)
minimumList([X|Q], M) :- % We 'cut' our list to have one element, and the rest in Q
minimumList(Q, M1), % We call our predicate again with the smallest list Q, the minimum will be in M1
M is min(M1, X). % We check if our first element X is smaller than M1 as we unstack our calls
Similar to andersoj, but using a cut instead of double comparison:
min([X], X).
min([X, Y | R], Min) :-
X < Y, !,
min([X | R], Min).
min([X, Y | R], Min) :-
min([Y | R], Min).
Solution without "is".
min([],X,X).
min([H|T],M,X) :- H =< M, min(T,H,X).
min([H|T],M,X) :- M < H, min(T,M,X).
min([H|T],X) :- min(T,H,X).
thanks for the replies. been useful. I also experimented furthur and developed this answer:
% if list has only 1 element, it is the smallest. also, this is base case.
min_list([X],X).
min_list([H|List],X) :-
min_list(List,X1), (H =< X1,X is H; H > X1, X is X1).
% recursively call min_list with list and value,
% if H is less than X1, X1 is H, else it is the same.
Not sure how to gauge how good of an answer this is algorithmically yet, but it works! would appreciate any feedback nonetheless. thanks!
min([Second_Last, Last], Result):-
Second_Last < Last
-> Result = Second_Last
; Result = Last, !.
min([First, Second|Rest], Result):-
First < Second
-> min([First|Rest], Result)
; min([Second|Rest], Result).
Should be working.
This works and seems reasonably efficient.
min_in_list([M],M).
min_in_list([H|T],X) :-
min_in_list(T,M),
(H < M, X = H; X = M).
min_list(X,Y) :- min_in_list(X,Y), !.
smallest(List,X):-
sort(List,[X|_]).
% find minimum in a list
min([Y],Y):-!.
min([H|L],H):-min(L,Z),H=<Z.
min([H|L],Z):-min(L,Z),H>=Z.
% so whattaya think!