简体   繁体   中英

Prolog predicate to unzip lists

I have written a prolog predicate my_unzip/2 that should unzip lists of any size. I can't seem to get the correct output and I am new to prolog.

my_unzip([], [[],[]]). 
my_unzip([X], [[X], []]). 
my_unzip([F, S | T], [[F|TS], [S|TF]]) :- my_unzip(T, [TS, TF]). 

example:

 my_unzip(X,[[a,b],[c,d]]).

should return

X = [[a,c],[b,d]]

but I am getting

X = [a, c, b, d]
my_unzip([  F, S | T], [[F|TS], [S|TF]]) :- my_unzip(T, [TS, TF]). 

gives you

X = [       a, c , .......

but you want

X = [      [a, c], .......

thus the needed change is, correspondingly,

my_unzip([ …F, S…| T], ......

(you'll need to make the corresponding change, replacing the s with the actual syntactic elements).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM