All Possible Matching Lists
Haskell, 49 characters
f[]=[[]]
f(2:r)=f(0:r)++f(1:r)
f(x:r)=map(x:)$f r
Interestingly this is exactly what I'd write even if this wasn't golf - except for removing spaces and calling the list's tail r
rather than xs
.
Haskell, 34 26 characters
Saved 8 characters thanks to Zgarb.
r 2=[0,1]
r n=[n]
f=mapM r
Prolog, 62 characters
f([],[]). f([H|T],[A|B]):-((H=2,(A=0;A=1));(H<2,A=H)),f(T,B).
Example:
?- f([0,2,0,2],X).
X = [0, 0, 0, 0] ;
X = [0, 0, 0, 1] ;
X = [0, 1, 0, 0] ;
X = [0, 1, 0, 1] ;