-1

So basically I'm trying to find the second minimal value in a list, and the problem is that mino and secondMin return false, instead of numbers. When I do minlist(List, Min) manually, it returns Min = number, but I can't get sec to take on that value. Instead, it stays false. Can anyone help?

Second Minimum

secondMin(L, Min2):-
    minlist(L,Min),
    delete(L,Min,Li),                   %no prob ver
    mino(L, k),                 %return k as the 2nd minimum of L
    Min2 is k.                  %Min2 is k.
//////////some code here////////////

mino(List, sec):-

    sec is minlist(List, Min).              %min 2 is the minimum of Li

1 Answers1

2

You need to read a tutorial or get a textbook. Only arithmetic "functions" return values, all other predicates simply use one of the arguments for output if called in that mode.

So something like

minlist(List, Min)

instead of

Min is minlist(List)

It is a bit strange because you do it correctly several times at the start of the predicate and then suddenly you start doing it wrong.

As for "second smallest number in a list", this might be easier:

Second smallest element is the list is the second element in the sorted list.

second_smallest(List, SS) :-
    sort(List, [_,SS|_]).

This breaks down if the list is not ground, but so does your original attempt.