Prolog, building list with conditional clauses

前端 未结 4 1033
南旧
南旧 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

    you can use the if structure:

     -> (do_something) ; (do_something else)
    

    in this case:

    goal(Stuff):-
      do_something(X),
      if_something(Y)-> do_this(Y) ; true,
      always_do_this(Z).
    

    or you simply write two clauses like:

    goal(Stuff):-
      do_something(X),
      conditional_stuff(Y),
      always_do_this(Z).
    
    conditional_stuff(Y):-
      condition(Y),
      do_this(Y).
    
    conditional_stuff(_).
    

提交回复
热议问题