如何计算在 F# 中存储为字符串的表达式 [英] How can evaluate an expression stored as a string in F#

查看:16
本文介绍了如何计算在 F# 中存储为字符串的表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

let x = 5
let y = 10

let expr = Console.ReadLine()

expr

可以在控制台中键入 "x+y" 以存储在 expr 中的位置.

Where one might type "x+y" in the console to store in expr.

如何在 F# 中评估这样的语句?

How does one evaluate a statement like this in F#?

最终,我希望用户能够在网页上输入表达式或系统的一组规则,并将它们保存在数据库中,以便在 F# 库中的适当时间应用.我只是不知道如何将输入的字符串转换为 F# 中的函数值.

Ultimately, I want a user to be able to enter expressions, or a set of rules for a system, on a webpage and have them saved in a database to be applied at appropriate times in an F# library. I just don't know how to convert the entered string in to a function value in F#.

感谢您提供的任何帮助!

Thanks for any help you may provide!

亚当

推荐答案

如前所述,F# 没有 eval,但是如果您可以定义语法,则可以使用 F# 中的 Lex 和 Yacc 实现(fslex 和 fsyacc).

F# doesn't have eval, as mentioned, but if you can define the grammar, you can utilize the Lex and Yacc implementations in F# (fslex and and fsyacc).

作为快速跟进,我知道在 ocaml 中,您可以使用 ocamlmktop 向您的用户利用交互式控制台.我不确定 F# 中的等效项.不过,这似乎与您想要的网络界面不匹配(对吗?).

As a quick follow up, I know in ocaml you can exploit the interactive console to your users with ocamlmktop. I am unsure of an equivalent in F#. This, although, doesn't seem to match what you want with a web interface (correct?).

这篇关于如何计算在 F# 中存储为字符串的表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆