Saturday, 15 February 2014

List as variable/argument in Prolog -


I have created a function in the prologue to "turn on" a list, e.g. Add tail to the list of favorites so much:

  - turn ([a, b, c, d, e], tlist). In terms of my program, I want to be able to use the predefined list for the rule, such as  
  alist ([a, b, c, d, e, f])  

But I get many different errors. I have tried the following arguments:

  turn (bye (L), R). Listv (X): - Bye (L), Member (X, L) turn (listv (X), R).  

And I think that each of these lists has different representations of the list according to prologue, but I'm not sure which list representation is appropriate for completing the operation on a predefined list. is.

Thank you!

This code can be defined by easily turn / 2 Is:

  turn ([A | as], BS): - Annex (e.g., [A], BS). To specify some sample lists, we define a subsidiary predicate  name_list / 2 : 

  named list (one_to_nine, [1, 2,3, 4,5,6,7,8,9]). Name_list (a_to_f, [a, b, c, d, e, f]) Name_list (single_degit_primes, [2,3,5,7])  

Let's query!

? - Named list (a_to_f,  xs ), turn (xs, ys). Xs = [ one , b, c, d, e, f], ys = [b, c, d, e, f,  a ]. Is successful in determining? - Named list (a_to_f,  ys ), turn (xs, ys). "Other" direction Ys = [a, b, c, d, e,  f ], x = [ f , a, b, c, d, e]% The option succeeds in leaving behind; false. 

So far, we have used a specific sample list in the query a_to_f ; Let's use all of these!

? - Named list ( name , x), turn (xs, ys) Name = one_to_nine, Xs = [ 1 , 2,3,4,5,6,7,8,9], YS = [2,3,4,5,6,7,8, 9,  1 ]; Name = a_to_f, Xs = [ a , b, c, d, e, f], ys = [b, c, d, e, f,  a ]; Name = single_digit_primes, x = [ 2 , 3,5,7], ys = [3,5,7,  2 ]. 

No comments:

Post a Comment