Prolog, 2015, Exercitii

download Prolog, 2015, Exercitii

of 3

Transcript of Prolog, 2015, Exercitii

  • 7/24/2019 Prolog, 2015, Exercitii

    1/3

    Exercise 6.1

    Let's call a list doubledif it is made of two consecutive blocks of elements that are exactly the

    same. For example, [a,b,c,a,b,c] is doubled (it's made up of [a,b,c]followed by [a,b,c])

    and so is [foo,gubble,foo,gubble]. On the other hand, [foo,gubble,foo] is not doubled.

    rite a predicate doubled(List)which succeeds when Listis a doubled list.

    doubled(List) :- append(X,X,List).

    Exercise 6.2

    ! palindrome is a word or phrase that spells the same forwards and backwards. For example,

    "rotator#, "eve#, and "nurses run# are all palindromes. rite a predicate palindrome(List),

    which checks whether List is a palindrome. For example, to the $ueries

    ?- palindrome([r,o,t,a,t,o,r]).

    and

    ?- palindrome([n,u,r,s,e,s,r,u,n]).

    %rolo& should respond "yes#, but to the $uery

    ?- palindrome([n,o,t,h,i,s]).

    %rolo& should respond "no#.

    palindrome(List) :- reverse(List,List).

    Exercise 6.3

    . rite a predicate second(X,List)which checks whether Xis the second element of List.

    second(X,[,X!]).

    . rite a predicate s"ap#$(List#,List$)which checks whether List#is identical to List$,except that the first two elements are exchan&ed.

    s"ap#$([X,%!&],[%,X!&]).

    . rite a predicate final(X,List)which checks whether Xis the last element of List.

  • 7/24/2019 Prolog, 2015, Exercitii

    2/3

    final(X,List) :- reverse(List,[X!]).

    *. rite a predicate toptail('nList,utlist)which says "no# if i'nlistis a list containin&

    fewer than elements, and which deletes the first and the last elements of 'nlistand returns

    the result as utlist, when 'nlistis a list containin& at least elements. For example+

    toptail([a],&).

    no

    toptail([a,b],&).

    &[]

    toptail([a,b,c],&).

    &[b]

    int+ here#s where appendcomes in useful.

    toptail([!Xs],utlist) :- append(utlist,[],Xs).

    -. rite a predicate s"apfl(List#,List$)which checks whether List#is identical to List$,

    except that the first and last elements are exchan&ed. int+ here's where appendcomes in

    useful a&ain.

    s"apfl([X!Xs],List$) :-

    append(&,[*],Xs),

    append([*!&],[X],List$).

    Exercise 6.4

    !nd here is an exercise for those of you who, like me, like lo&ic pules.

    /here is a street with three nei&hborin& houses that all have a different color. /hey are red, blue,

    and &reen. %eople of different nationalities live in the different houses and they all have a

    different pet. ere are some more facts about them+

    /he 0n&lishman lives in the red house.

    /he 1a&uar is the pet of the 2panish family.

    /he 3apanese lives to the ri&ht of the snail keeper.

    /he snail keeper lives to the left of the blue house.

  • 7/24/2019 Prolog, 2015, Exercitii

    3/3

    ho keeps the ebra4

    5efine a predicate +ebra#that tells you the nationality of the owner of the ebra.

    int+ /hink of a representation for the houses and the street. 6ode the four constraints in %rolo&.

    memberand sublistmi&ht be useful predicates.

    neighbor(L,,[L,!]).

    neighbor(L,,[!Xs]) :- neighbor(L,,Xs).

    +ebra(X) :-

    treet [*#,*$,*/],

    member(house(red,englishman,), treet),

    member(house(,spanish,0aguar), treet),

    neighbor(house(,,snail), house(,0apanese,), treet),

    neighbor(house(,,snail), house(blue,,), treet),

    member(house(,X,+ebra),treet).