Prolog, building list with conditional clauses

前端 未结 4 1045
南旧
南旧 2021-01-12 02:46

I need to do this homework assignment using prolog (SWI-flavor) and cant get my head around some things.

For example, if i want to iterate through a list and add it

4条回答
  •  深忆病人
    2021-01-12 03:02

    If I understand correctly, then what you need is a predicate like include/3:

    include(:Goal, +List1, ?List2)
        Filter elements for which Goal succeeds. True if List2 contains
        those elements Xi of List1 for which call(Goal, Xi) succeeds.
    

    Usage example:

    ?- include(number, [a(b), 2, _, 1.2, C, '1'], L).
    L = [2, 1.2].
    

    Now your homework becomes "how to implement include/3". Once you have implemented your version of include/3 you can check if it matches SWI's version by looking at its source code: listing(include).

提交回复
热议问题