prolog - Convert list into functor parameter -
i got stuck implement logic. @ instance in program have list named list. length of list variable , don't know in advance. have pass list in functor create fact , unable implement it. eg:
if list [first] should add fact functor(first).
if list [first,second] should add fact functor(first,second).
if list [first,second,third] should add fact functor(first,second,third).
, on...
i trying =.. here unable map variable length constraint. fixed length able perform don't know in advance how many elements there in list.
any suggestions implement logic. thanks.
i don't quite understand problem =.. worked me:
assert_list(list) :- term =.. [my_functor|list], assert(term). note use my_functor instead of functor because functor/3 built-in predicate cannot assert ternary functor facts (functor(first, second, third)).
calling it:
?- assert_list([first,second,third]). true. checking works:
?- listing(my_functor). :- dynamic user:my_functor/3. user:my_functor(first, second, third). true. note technically, different n-ary my_functor/n predicates not same predicates. must use different queries in program each n. circumvent this, assert list 1 , argument of my_functor:
?- list = [first, second, third], assert(my_functor(list)). true. ?- listing(my_functor). :- dynamic user:my_functor/3. user:my_functor([first, second, third]). true. my swi-prolog version 5.7.5.
Comments
Post a Comment