recursion - mirroring a list in prolog -


my wanted output this:

?- mirror ([1,2], [] , x ). x= [1,2,2,1] 

what have far:

mirror(l,r,x):- l r , [r| revertlist(l,x)] . 

i cant think of how works, please me

it not different reversing list, how write not going work. googled "prolog is" , after maybe 10 seconds see is/2 arithmetic expressions. don't know how think can put predicate maybe not possible? if want append can use append append mirror reversed list end of original list final "mirror" result:

mirror(x, y) :- reverse(x, r), append(x, r, y). 

but easy? wonder maybe there more question? don't know why have 3 arguments when need 2 arguments? maybe thought can use accumulator reverse list because reverse list use accumulator this?

list_rev(l, r) :- list_rev(l, [], r).  list_rev([], r, r). list_rev([x|xs], ys, r) :-     list_rev(xs, [x|ys], r). 

but easy google, googled , found it, maybe googled , didn't it? "mirrored" need keep original list too, so:

list_mirrored(l, m) :- list_mirrored(l, [], m).  list_mirrored([], m, m). list_mirrored([x|xs], ys, [x|zs]) :-     list_mirrored(xs, [x|ys], zs). 

i wasn't sure if correct , googled "prolog append" , how done.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -