Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun pretvori(pair: pattern) =
- case pair of
- VariableP a => Variable a
- | ConstantP a => Constant a
- | PairP [a,b] => Pair [pretvori(a), pretvori(b)]
- | OperatorP(a, b) => Operator(a, (pretvori(b)))
- | ListP a => List (List.map (fn x => pretvori(x)) a )
- fun match(ex: expression, pt: pattern) =
- case pt of (*najdemo prvi pattern*)
- (*--------------------------------------WILDCARD--------------------------------------------///*)
- Wildcard => SOME nil
- (*--------------------------------------VARIABLE-------------------------------------------///*)
- |VariableP s => let
- fun ifExists(s) =
- case ex of
- Constant x => SOME[(s, Constant x)]
- | Variable y => SOME[(s, Variable y)]
- | Operator z => SOME[(s, Operator z)]
- | Pair w => SOME[(s, Pair w)]
- | List l => SOME[(s, List l)]
- in
- ifExists(s)
- end
- (*--------------------------------------CONSTANT-------------------------------------------///*)
- | ConstantP x => let
- fun ifExists(x) =
- case ex of
- Constant y => if(x = y) then SOME nil else NONE
- | _ => NONE
- in
- ifExists(x)
- end
- (*--------------------------------------PAIR----------------------------------------------*)
- | PairP [a1, a2] => let
- fun ifExists(a1, a2) =
- case ex of
- Pair [b1,b2] => if(b1 = pretvori(a1) andalso b2 = pretvori(a2)) then SOME [("merged", Pair [b1, b2])] else NONE
- | _ => NONE
- in
- ifExists(a1, a2)
- end
- (*--------------------------------------LIST----------------------------------------------*)
- | ListP ps => let
- fun ifExists(ps) =
- case ex of
- List xs => if(List xs = pretvori(ListP ps)) then SOME [("SFAS", List xs)] else NONE
- | _ => NONE
- in
- ifExists(ps)
- end
- (*--------------------------------------OPERATOR----------------------------------------------*)
- | OperatorP(a, PairP([x1, x2])) => let
- fun ifExists(a, x1, x2, ex) =
- case ex of (* Primerjamo pattern z expressiono*)
- Operator(b, Pair([y1, y2])) => if(a = b) then SOME[("x1", y1), ("x2", y2)] else NONE
- | _ => NONE
- in
- ifExists(a, x1, x2, ex)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement