2
votes

I have this Prolog code:

pick_number_simple([],[]).
pick_number_simple([H|T],[H|T2]):- 
    number(H), pick_number_simple(T,T2).
pick_number_simple([H|T],T2):-
    not(number(H)), pick_number_simple(T,T2).

which gets the numbers out a list. For example:

pick_number_simple([d,f,7,5,e,3,g], NumList)

gives you:

[7,5,3]

But I want to make it get the numbers off of a nested list.

For example:

pick_numbers_general([a,b,1,[2,[c,3]],d],1,NumList)

Would give you:

[2,3]

how do I do this?

1
Wait. What means "make it get the numbers off of a nested list"? And what should do second argument of pick_numbers_general/3 ? - ДМИТРИЙ МАЛИКОВ
as in to get the numbers out of a nested list for example if i have this pick_numbers_genera([a,b,1,[2,[c,3]],d],1,NumList). and i want to get Numlist = [2,3]. - guest
From nested list only? What for "1" is in this example? - ДМИТРИЙ МАЛИКОВ
it doesn't have to be nested list only, and 1 is a number that if the output sis greater than it, then the predicate is true. - guest
I've updated my answer. Is it what u need? - ДМИТРИЙ МАЛИКОВ

1 Answers

1
votes

U may use flatten/2 predicate

pick_number_simple([],[]).
pick_number_simple([H|T],[H|T2]):- 
    number(H), pick_number_simple(T,T2).
pick_number_simple([H|T],T2):-
    not(number(H)), pick_number_simple(T,T2).

pick_numbers_general(List, Num, NumList) :-
    flatten(List, NestList),
    pick_number_simple(NestList, NumListAll),
    findall(X, (member(X, NumListAll),X > Num), NumList).

So

?- pick_numbers_general([a,b,1,[2,[c,3]],d],1,NumList).
NumList = [2, 3] ;