一个匹配模式中的多个操作Ocaml

时间:2017-08-06 15:01:30

标签: ocaml

我之前解决了Ocaml问题,但我想知道是否有最好的方法来优化我的解决方案,因为我已经尝试了所有的途径,因为我对Ocaml很新,我无法弄明白。我有一个模式功能,可以进行漂亮的打印,然后是另一个添加到集合中的函数。漂亮的打印功能如下:

let rec processoperatorchange fmt = function
| Zero -> Format.fprintf fmt "0"
| Pproc x -> Format.fprintf fmt "%s" x
| Procdef (p1, x) -> Format.fprintf fmt "%s(%s)"  p1  x
| Par (p1, p2) -> Format.fprintf fmt "(%a + %a)" processoperatorchange p1 processoperatorchange p2
| Concur(p1, p2) -> Format.fprintf fmt "(%a | %a)" processoperatorchange p1 processoperatorchange p2
| Rep(p) -> Format.fprintf fmt "!(%a)" processoperatorchange p

我有另一个功能做同样的事情,但后来改变了打印的格式(这似乎效率低下,但我无法找出使这更好的最佳方法)

let rec processoperatorchange2 fmt = function
| Zero -> Format.fprintf fmt "0"
| Pproc x -> Format.fprintf fmt "%s" x
| Procdef (p1, x) -> Format.fprintf fmt "%s(%s)"  p1  x
| Par (p1, p2) -> Format.fprintf fmt "(%a | %a)" processoperatorchange2 p1 processoperatorchange2 p2
| Concur(p1, p2) -> Format.fprintf fmt "(%a + %a)" processoperatorchange2 p1 processoperatorchange2 p2
| Rep(p) -> Format.fprintf fmt "!(%a)" processoperatorchange2 p

基于这些函数,我有另一个函数调用添加到集合:

let op_change p set = concattoset (Format.asprintf "%a" processoperatorchange p) set |>
                    concattoset (Format.asprintf "%a" processoperatorchange2 p)

concattoset的代码是:

let concattoset s set = SS.add s set

但是,有没有一种方法可以优化这段代码,这样我就可以将两个函数processoperatorchangeprocessoperatorchange2压缩成一个函数并将所有这些函数添加到集合中?这里的主要问题是我需要对来自用户的用户定义输入执行更改,然后将此更改添加到集合中,如果没有更改,则返回一个空集但是现在已完成的方式,如果有没有变化我仍然添加到集合因为我执行函数调用以单独添加到集合但我可以将所有这些压缩成一个函数吗?

1 个答案:

答案 0 :(得分:2)

from flask import .... from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) # database stuff deleted to keep it short class Person(db.Model): __tablename__ = "persons" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text) nickname = db.Column(db.Text) def __init__(self, name, nickname): self.name = name self.nickname = nickname self.age def set_age(self,age): self.age = age @app.route("/") def index(): return render_template("index.html") @app.route("/register", methods=["POST"]) def register(): if request.form["name"] == "" or request.form["nickname"] == "": return render_template("failure.html") person = Person(request.form["name"], request.form["nickname"]) db.session.add(person) db.session.commit() return render_template("add_age.html", person = person) @app.route("/add_age") def add_age(): age = 23 person.set_age = age return redirect(url_for("index")) Par使用的字符外,您的功能相同。您可以将这些字符作为参数传递。这给你一个像这样的函数:

Concur

如果你这样调用这个函数:

let rec processoperatorchange pc cc fmt = function
| Zero -> Format.fprintf fmt "0"
| Pproc x -> Format.fprintf fmt "%s" x
| Procdef (p1, x) -> Format.fprintf fmt "%s(%s)"  p1  x
| Par (p1, p2) ->
    Format.fprintf fmt "(%a %c %a)"
        (processoperatorchange pc cc) p1 pc (processoperatorchange pc cc) p2
| Concur(p1, p2) ->
    Format.fprintf fmt "(%a %c %a)"
        (processoperatorchange pc cc) p1 cc (processoperatorchange pc cc) p2
| Rep(p) ->
    Format.fprintf fmt "!(%a)"
        (processoperatorchange pc cc) p

它的行为与您原来的processoperatorchange '+' '|' 功能相同。如果你这样称呼它:

processoperator

它的行为类似于processoperatorchange '|' '+'

所以你可以像这样重写processoperator2

op_change
相关问题