我是新来的Prolog和我有麻烦搞清楚如何找到包含数字和字母列表中的第二个最低寻找第二最小值。当它包含一个字母时,它应该显示一个错误,当它有一个数字时,它应该显示一个错误。到目前为止,我有这个,但我不知道如何开始我的编码具有信件...SWI-Prolog的:在列表中
secondMin([_]) :-
print("ERROR: List has fewer than two unique elements."),
!.
secondMin(List, M2) :-
min_list(List, M1),
delete(List, M1, List1),
min_list(List1, M2).
输出应该是这样的:
?- secondMin([17,29,11,62,37,53], M2).
M2 = 17
?- secondMin([512], M2).
ERROR: List has fewer than two unique elements.
?- secondMin([7,5.2,3,6,-3.6,9,-2], M2).
M2 = -2
?- secondMin([12,2,b,7], M2).
ERROR: "b" is not a number.
?- secondMin([3,3,3], M2).
ERROR: List has fewer than two unique elements.
如果它所具有既不是一个整数,也不是字母元素?例如'abc'或'1.23'? – lurker
对不起,我不是故意说整数只是任何唯一的数字 – tadashi
“第二最小”听起来很好定义。然而,secondMin([1,1,2,3],M)'现在是'M = 1'还是'M = 2'? – false