函数式编程和方程求解器
就像个人实验一样,为了更好地学习编程和形式语言理论等,我正在尝试编写一种基本上接受一堆方程并或多或少自动或启发式求解未知数的语言。 我试图通过用 C 语言编写一个解释器来做到这一点。
不过,所有这些都不是超级相关的......更重要的是,我在过去几天发现了函数式编程(我的意思是我阅读了维基百科的条目)和一个简短的 Haskell 教程),它似乎处理与我想做的事情非常相似的事情。
我想我想知道的是,是否有其他我应该研究的语言,或者任何具有尝试做类似事情的库或程序的非功能性语言,这样我就可以更好地理解我的意思打算做什么?
另外,有没有关于编写解释器等的好的参考资料?
谢谢。
PS哦,我知道我可以而且应该使用谷歌。 我,在一边。 最重要的是,我正在寻找关于什么是好的以及人们以前使用过的“第二意见”的集体。 另外,因为我是新来的,所以我想更好地了解这个社区。 谢谢你的耐心 :-)
Just as a personal experiment, in order to try to learn better about programming and formal language theory and the like, I'm trying to write a language that basically takes in a bunch of equations and solves for unknowns more or less automatically or heuristically. I'm trying to do this by writing an interpreter in C.
All that's not super-relevant, though... more importantly, I've discovered functional programming in the last few days (by which I mean I read Wikipedia's entry for it and a brief Haskell tutorial) and it seems to deal with very similar things to what I want to do.
I guess what I'm wondering is, are there any other languages I should look into, or any non-functional languages that have libraries or programs that try to do similar things, such that I might gain a better understanding of what I'm setting out to do?
Also, are there any good references out there for writing interpreters, etc.?
Thanks.
P.S. Oh, and I'm aware I could and should use Google. I am, on the side. More than anything I'm looking for a collective of "second opinions" for what's good, and what people have used before. Also, I'm trying to get to know the community a little better, since I'm new here. Thanks for your patience :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
免责声明:我没有认真探索这个领域,但希望这篇小文章对您有用 - 并等待看到其他人的更多答案。
我认为其中有多个问题:
1)方程求解器。
如果你的意思是象征性地“解决未知数” - 这是你即将开始的一大堆工作,恕我直言:-)你将开始创建一个 计算机代数系统。
术语重写本身就是一个相当大的话题。 如果您对这个级别的操作特别感兴趣,C 可能不是最容易使用的 - 您可能会更轻松地使用 Lisp 来完成该任务。
值得注意的是,并不是每组方程都会有解,并且“仅仅”弄清楚它是否有解本身就是一项艰巨的任务。
另一方面,如果您希望以数值方式求解方程,类似 this 可能会看起来很有趣。
2)一般的函数式编程。
Haskell 是一种很棒的语言(尽管我仍然是一个初学者 - 我认为它可能是最优雅的语言之一)。 OCaml 可能是另一条值得探索的道路。 然后,当然还有Scheme。 如果您正在处理 Web 编程,那么具有直接实际意义的语言可能是 XSLT。
当然,您可以轻松地用 Ruby 和 Python 编写函数式风格。 观察学习新语言如何改变“主要”语言的整体编程模式是非常有趣的。 所以,无论是否有语言理论——你接触的语言越多越好。
3)编写解释器等。
我怀疑考虑到问题的风格,您想做的最有趣的实际应用不是解释器,而是编译器中的优化代码。 为此 - 龙书 和 麻省理工学院计算机语言工程课程会很有用。 然后你可以获取一份 TCC 并使用它。 如果你想尝试一些不那么传统的东西,请看看 potion - 一个非常好的有趣的语言实验,将 x86 机器代码作为其“字节码”(因此 x86 机器上的性能非常惊人)。
这个问题实际上引用了来自 ( 3) 上面,还有很多。
Disclaimer: I did not seriously explore this field, but hope this small write-up might be useful to you - and awaiting to see more answers from the others.
I think there are multiple questions in one:
1) Equation solvers.
if you mean "solving for unknowns" symbolically - it's a pretty big pile of work that you are about to get started with, IMHO :-) You're about to embark on creating a computer algebra system.
Term rewriting is a rather big topic on its own. If you are specifically interested in the manipulations at this level, C might be not the easiest to work with - you'd probably be more at ease with Lisp for that task.
Notably, not every set of equations is going to have a solution - and "just" figuring out the fact whether it does have a solution or not is a tough task on its own.
On the other hand, if you look up to solving the equations numerically, something like this might be interesting to look at.
2) Functional programming in general.
Haskell is a great language for that (although I am still a very beginner in it - I think it might be one of the most elegant ones). OCaml might be another path to explore. Then, of course, there is Scheme. A language with immediate practical implications could be XSLT if you are dealing with web programming.
And of course you can easily write functional style in Ruby and Python. It is very interesting to observe how learning the new languages changes your programming patterns in "main" language overall. So, language theory or not - the more languages you touch, the better.
3) Writing the interpreters, etc.
I suspect that given the flavour of the question, the most interesting practical application for what you would like to do would be not the interpreter, but the optimization code in the compiler. For that - Dragon book and MIT computer language engineering course would be useful in my opinion to start with. Then you could grab e.g. a copy of TCC and play with it. If you want to tinker with something less conventional, take a look at potion - a very interesting language experiment that has x86 machine code as its "bytecode" (hence the performance on x86 machines is pretty spectacular).
This question on SO actually references most of the of the links from (3) above, and quite a few more.
正如安德鲁所建议的,您所描述的核心被称为“计算机代数系统”,更一般地说是“术语重写系统”。 阅读这两个领域的内容应该会让你有所了解。
是的,我希望您会发现函数式编程是一个非常适合的范例。 也许逻辑编程也是如此。
我的预感是,你关于口译员的问题有些操之过急,并且来自格林斯潘的第十条规则 ,即
您可以将“Common Lisp”替换为“函数式编程语言”。
换句话说,如果您选择并采用一种有意识设计的函数式编程语言(用于您的计算机代数系统),您就不必无意识地(而且很糟糕)创建一种语言。
在函数式语言中我个人最喜欢的是 Haskell。 对于新手来说,Haskell 的优势在于,当您仍在命令式编程时,通过静态类型,您会承认这一点。
As Andrew suggested, the heart of what you describe is known as "computer algegra systems", and more generally "term-rewriting systems". Reading up on those two areas ought to get you oriented.
And, yes, I expect you'll find functional programming a well-suited paradigm. Perhaps logic programming as well.
My hunch is that your question about interpreters is jumping the gun and results from Greenspun's Tenth Rule, which is
where you might replace "Common Lisp" with "a functional programming language".
In other words, if you choose and embrace a consciously designed functional programming language (for your computer algebra system), you won't have to create one unconsciously (and badly).
My personal favorite among functional languages is Haskell. For newbies, Haskell has the advantage making you admit--via static typing--when you're still programming imperatively.