模式匹配某些列表-不同的情况

时间:2018-06-25 08:29:47

标签: list prolog pattern-matching unification

带有列表的不同案例。如果这些案例(a)到(h)是否有效。如果有效,则应给出结果。我所做的是

(objectclass=group)(cn:member:Bob Johnson)

1 个答案:

答案 0 :(得分:0)

统一是指匹配术语的相应部分。

(a) ?- [ s , u ,   b,s,t,i,t,u,t,e  ] =
       [ s , u  | [b,s,t,i,t,u,t,e] ] =
       [ H1, H2 | T                 ] 

(b) ?- [ substitute      ] =
       [ substitute | [] ] =
       [ H1         | T  ] 

(c) ?- [ [substitute]      ] =
       [ [substitute] | [] ] =
       [ H1           | T  ]

(d) ?- [ [s,u,b,s] ,   [t,i,t,u,t,e]   ] =
       [ [s,u,b,s] | [ [t,i,t,u,t,e] ] ] =
       [ H1        | T                 ]

(e) ?- [ [s,u,b,s] ,  t,i,t,u,t,e  ] =
       [ [s,u,b,s] | [t,i,t,u,t,e] ] =
       [ H1        | T             ]

(f) ?- [ [s,u,b,s] , [t,i,t,u,t,e]      ] =
       [ [s,u,b,s] , [t,i,t,u,t,e] | [] ] =
       [ H1        , H2            | T  ]

(g) ?- [ [s , u , b, s ], [t,i,t,u,t,e]      ] =
       [ [s , u , b, s ], [t,i,t,u,t,e] | [] ] =
       [ [H1,H2 ]       , H3            | T  ]       % fails!

(e) ?- f1( a, Y    , f(Z, 2)) =
       f1( a, [1,2], f(1, Y)) 

(h) ?- f2( A, B            , C) =
       f2( a, [1,2,f(1,Y)] )                          % fails!