in Education by
I'm new to prolog and i can't find a solution to filter list by 2 conditions, save result into 2 variables and then evaluate answer base on them. In my case, if list contains more numbers that greater then 0, then numbers lesser then 0. For example i get a list with elements: checklist([-2, 3.3, 12, -10.1, 14, -11, 123]) # true checklist([-2.1, -3, -4, 14, 16.7]) # false checklist([11.5, 2.5, -34.1, -1]) # false I'd write something like this in python: bigger_count = 0 lesser_count = 0 for num in list: if num > 0: bigger_count += 1 elif num < 0: lesser_count += 1 print(bigger_count > lesser_count) Especially i doesn't understand how to work with 2 variables at one time. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
https://www.swi-prolog.org/pldoc/doc_for?object=include/3 https://www.swi-prolog.org/pldoc/doc_for?object=convlist/3 If you have 2 conditions, you need to traverse the list twice, but is the complexity of code that can handle two different predicates worth the trouble? Anyway, if you're writing from scratch, you can simplify your life by using DCGs: filter(_Pred, []) --> []. filter(Pred, [X|Xs]) --> ( { call(Pred, X) } -> [X] ; [] ), filter(Pred, Xs). filter(Pred, List, Result) :- phrase(filter(Pred, List), Result). even(X) :- 0 is X mod 2. ?- filter(even, [1,2,3,4,5], X). X = [2, 4]

Related questions

0 votes
    I'm new to prolog and i can't find a solution to filter list by 2 conditions, save result into ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I'm new to prolog and i can't find a solution to filter list by 2 conditions, save result into ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I have a situation where i have a list of primary keys. And i need to filter(OR) a queryset ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    Consult the table of standard electrode potential and suggest three substances that can oxidize Fe2+ ions under suitable conditions. Select the correct answer from above options...
asked Jan 5, 2022 in Education by JackTerrance
0 votes
    If access paths are available on all the conditions of a disjunctive selection, each index is scanned ... Operation in division Query Processing Techniques of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    If a robot can alter its own trajectory in response to external conditions, it is considered to be __________ (a) ... open loop (d) non-servo Please answer the above question....
asked Oct 22, 2022 in Education by JackTerrance
0 votes
    If a robot can alter its own trajectory in response to external conditions, it is considered to be __________ (a) ... open loop (d) non-servo Please answer the above question....
asked Oct 2, 2022 in Education by JackTerrance
0 votes
    Which natural calamities have you experienced? How did they affect the conditions in your surroundings? ... proposed by,electromagnetic theory engineering physics,Science nptel...
asked Nov 7, 2021 in Education by JackTerrance
0 votes
0 votes
    Two series resonant filters are shown below. Let the 3 dB bandwidth of filter 1 be B1 and that of filter 2 be ... for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 19, 2021 in Education by JackTerrance
...