私はRacketを習得しようとしていますが、その過程でPythonフィルタを書き直そうとしています。私は私のコード内の関数の次のペアを持っている:PythonからRacketへの移行(正規表現ライブラリと "Racket Way")
def dlv(text):
"""
Returns True if the given text corresponds to the output of DLV
and False otherwise.
"""
return text.startswith("DLV") or \
text.startswith("{") or \
text.startswith("Best model")
def answer_sets(text):
"""
Returns a list comprised of all of the answer sets in the given text.
"""
if dlv(text):
# In the case where we are processing the output of DLV, each
# answer set is a comma-delimited sequence of literals enclosed
# in {}
regex = re.compile(r'\{(.*?)\}', re.MULTILINE)
else:
# Otherwise we assume that the answer sets were generated by
# one of the Potassco solvers. In this case, each answer set
# is presented as a comma-delimited sequence of literals,
# terminated by a period, and prefixed by a string of the form
# "Answer: #" where "#" denotes the number of the answer set.
regex = re.compile(r'Answer: \d+\n(.*)', re.MULTILINE)
return regex.findall(text)
私はラケットの最初の関数の実装は、次の線に沿って何かだろう伝えることができるものから:動作しているように見えます
(define (dlv-input? text)
(regexp-match? #rx"^DLV|^{|^Best model" text))
正しく第二の機能の実装に取り組んで、私は現在、以下の(で開始する)が出ている:
(define (answer-sets text)
(cond
[(dlv-input? text) (regexp-match* #rx"{(.*?)}" text)]))
regexp-match*
を含めて、正規表現にマッチする文字列のリストを与えるので、これは正しくありません。中括弧。誰もがPythonの実装と同じ振る舞いをする方法を知っていますか?また、正規表現を「より良く」する方法の提案もあれば幸いです。
マイグレーションの原因を尋ねることがありますか? –
私はPythonを学ぶためにオリジナルのフィルタを書いていて、Racketを学ぶのと同様のアプローチを取っています。だから、それはちょっと個人的なものだから "私は理由を挙げたい"。 – ggelfond
注記:動作を正確に一致させるために、Pythonのバージョンでは、コンパイルされた正規表現の値を持ち上げる必要がありました。 Pythonは単純な入力に対して 're.compile'の結果をキャッシュすることがありますが、パフォーマンスが重大なコードに対してはこの動作に依存したくないかもしれません。 – dyoo