LAB: Working with lists in Prolog (part 2)

1. Problems to solve with lists

Using the knowledge gained in the previous lab, solve the following problems:

  1. Define a predicate that removes the last 3 elements from a list L, the results is list L1, use the list_merge predicate from the previous lab.
  2. Define a predicate that removes the first 3 elements from a list L, the results is list L1, use the list_merge predicate.
  3. Define a predicate that removes the first 3 and the last 3 elements from a list L, the results is list L2, use the list_merge predicate.
  4. Define a pair of predicates (complementary, i.e., one use the other) odd(L) and even(L), that check if theirs argument list as odd or even length.
    • is your predicate able to also create a list of even or odd length (we put a variable as the argument, not a constant)
  5. Define a predicate palindrome(L), L is a palindrome, if it read backwards the same as forward, np. [a,l,a], [m,a,d,a,m]. (hint: you can (but don't have to) use the predicate list_reverse.)
  6. Define a predicate list_shift(L1,L2), where L2, is like L1 shifted by one element, e.g.:
          ?- list_shift([1,2,3,4,5,6,7,8],X),list_shift(X,Y),list_shift(Y,Z).
     
          X = [2, 3, 4, 5, 6, 7, 8, 1]
          Y = [3, 4, 5, 6, 7, 8, 1, 2]
          Z = [4, 5, 6, 7, 8, 1, 2, 3]
  7. Define a predicate list_translate(L1,L2), that translates a list of numbers (max. 0-9),to a list of words:
          ?- list_translate([1,4,7],X).
     
          X = [one, four, seven] ;
     
          ?- list_translate(A,[two,eight,zero]).
     
          A = [2, 8, 0] ; 

    using the facts:

          means(0,zero).   means(1,one).
          means(2,two).    means(3,three).
          means(4,four). means(5,five).
          means(6,six).  means(7,seven).
          means(8,eight).  means(9,nine).

    Hint: use recursion.

  8. Define a predicate list_subset(L,Z), that checks if Z is a subset of L, and writes out all the possible subsets of L, if Z is z variable.
          ?- list_subset([a,b,c],[c]).
     
          Yes
          ?- list_subset([a,b,c],[a,c]).
     
          Yes
          ?- list_subset([a,b,c],X).
     
          X = [a, b, c] ;
          X = [a, b] ;
          X = [a, c] ;
          X = [a] ;
          X = [b, c] ;
          X = [b] ;
          X = [c] ;
          X = []
  9. Define a predicate list_divide(L,L1,L2), that divides list L into 2 fragments, L1 and L2, more less the same length, e.g.:
          ?- list_divide([],X,Y).
     
          X = []
          Y = [] ;
     
          ?- list_divide([1],X,Y).
     
          X = [1]
          Y = [] ;
     
          ?- list_divide([1,2],X,Y).
     
          X = [1]
          Y = [2] ;
     
          ?- list_divide([1,2,3],X,Y).
     
          X = [1, 3]
          Y = [2] ;
     
          ?- list_divide([1,2,3,4],X,Y).
     
          X = [1, 3]
          Y = [2, 4] ;
     
          ?- list_divide([1,2,3,4,5],X,Y).
     
          X = [1, 3, 5]
          Y = [2, 4] ;
     
          ?- list_divide([1,2,3,4,5,6,7,8],X,Y).
     
          X = [1, 3, 5, 7]
          Y = [2, 4, 6, 8] ;
  10. Define a predicate list_flatten, that changes arbitrarily nestes list into a flat list (i.e, its elements are not lists)
          ?- list_flatten([[a],b,c],X).
     
          X = [a, b, c] 
     
          ?- list_flatten([[a],[b,[d]],c],X).
     
          X = [a, b, d, c] 
     
          ?- list_flatten([a,b,c],X).
     
          X = [a, b, c] 
     
          ?- list_flatten(a,X).
     
          X = [a]
  11. Write a program that calculates the coins into which a given amount of money can be exchanged.
    • Define possible coins, e.g: coin(1), coin(2), …
    • The predicate should have 2 arguments change_money/2, where the first argument is the amount to be changed, and the second is the list of coin nominals.
en/dydaktyka/intro2ai/labs/lab_prolog3.txt · Last modified: 2023/01/20 10:50 by ikaf
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0