在 C# 中使用 Lisp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/70004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:06:52  来源:igfitidea点击:

Using Lisp in C#

提问by Boris Callens

As a lot of people pointed out in this question, Lisp is mostly used as a learning experience. Nevertheless, it would be great if I could somehow use my Lisp algorithms and combine them with my C# programs. In college my profs never could tell me how to use my Lisp routines in a program (no, not writing a GUI in Lisp, thank you). So how can I?

正如很多人在这个问题中指出的那样,Lisp 主要用作学习经验。尽管如此,如果我能以某种方式使用我的 Lisp 算法并将它们与我的 C# 程序结合起来,那就太好了。在大学里,我的教授永远不会告诉我如何在程序中使用我的 Lisp 例程(不,不是在 Lisp 中编写 GUI,谢谢)。那我该怎么办?

采纳答案by jfs

Try these .Net implementations of Lisp:

试试这些 Lisp 的 .Net 实现:

IronScheme will aim to be a R6RS conforming Scheme implementation based on the Microsoft DLR.

IronScheme 旨在成为基于 Microsoft DLR 的符合 R6RS 的 Scheme 实现。

L Sharp .NET is a powerful Lisp-like scripting language for .NET. It uses a Lisp dialect similar to Arc but tightly integrates with the .NET Framework which provides a rich set of libraries.

L Sharp .NET 是一种强大的类似 Lisp 的 .NET 脚本语言。它使用类似于 Arc 的 Lisp 方言,但与提供丰富库集的 .NET Framework 紧密集成。

回答by Michiel Borkent

If it's merely the routines you want to use you might try LSharp, which lets you have Lisp expressions in .NET:

如果它只是您想使用的例程,您可以尝试 LSharp,它可以让您在 .NET 中拥有 Lisp 表达式:

http://www.lsharp.org/

http://www.lsharp.org/

The other way around (using .NET from Lisp) would be RDNZL:

另一种方式(使用 Lisp 中的 .NET)将是 RDNZL:

http://www.weitz.de/rdnzl/

http://www.weitz.de/rdnzl/

回答by Patrik Svensson

Perhaps you should take a look at L#. I don't know if it is what you are looking for (haven't touched Lisp since university) but it might be worth to check out.

也许你应该看看 L#。我不知道这是否是您正在寻找的(自大学以来就没有接触过 Lisp),但它可能值得一试。

http://www.lsharp.org/

http://www.lsharp.org/

回答by Lars Truijens

The .Net 1.1 SDK contains a LISP compiler example. See SDK\v1.1\Tool Developers Guide\Samples\clisp

.Net 1.1 SDK 包含一个 LISP 编译器示例。参见 SDK\v1.1\Tool Developers Guide\Samples\clisp

回答by cemerick

Clojureis a Lisp-1 that is compiledon-the-fly to Java bytecode, leading to very good runtime performance. You can use Clojure, and cross-compile it to a .NET assembly using IKVM's ikvmc. Of course, when used in .NET, Clojure happily generates .NET IL, leading to the same kind of compiled-code performance you can expect when using it on a JVM.

Clojure是一个 Lisp-1,可以即时编译为 Java 字节码,从而带来非常好的运行时性能。您可以使用 Clojure,并使用IKVM的 ikvmc将其交叉编译为 .NET 程序集。当然,当在 .NET 中使用时,Clojure 会愉快地生成 .NET IL,从而产生与在 JVM 上使用时相同的编译代码性能。

回答by Mark Hurd

There is also DotLisp.

还有DotLisp

回答by KFL

I know this is a really old question. But I'll try to provide an answer from my own experience and perspective.

我知道这是一个非常古老的问题。但我会尝试从我自己的经验和角度提供一个答案。

To those like us who love the pureness and elegance and simplicity of Scheme/Lisp, I hope this gives you some encouragement and inspiration how they can be very useful in real production :)

对于像我们一样喜欢 Scheme/Lisp 的纯粹、优雅和简单的人,我希望这能给你一些鼓励和启发,它们如何在实际生产中非常有用:)

I recently open-sourced a Scheme-like interpreter from work called schemy, written in C# (~1500 line of code). And here's the motivation and how it is useful -

我最近从名为schemy 的工作中开源了一个类似 Scheme 的解释器,用 C# 编写(约 1500 行代码)。这是动机以及它是如何有用的-

Without going into too much detail, I was building a web API server, whose request handling logic is desired to be plug-and-play by other developers/data scientists. There was a clear demand for separation of concern here - the server does not care much about the request handling logic, but it needs to know which requests it can handle and where to find and loadthe logic for the handlers.

没有详细说明,我正在构建一个 Web API 服务器,它的请求处理逻辑希望其他开发人员/数据科学家即插即用。这里有一个明确的关注点分离需求——服务器不太关心请求处理逻辑,但它需要知道它可以处理哪些请求以及在哪里查找和加载处理程序的逻辑。

So instead of putting handlers implementation in the server application, the server only provides re-usable "blocks" that can be chained together based on some criteria and logic to form a pipeline, i.e., handlers defined via configuration. We tried JSON/XML to describe such a pipeline and quickly realized that I was essentially building an abstract syntax tree parser.

因此,不是将处理程序的实现放在服务器应用程序中,服务器只提供可重用的“块”,这些块可以根据某些标准和逻辑链接在一起以形成管道,即通过配置定义的处理程序。我们尝试使用 JSON/XML 来描述这样的管道,并很快意识到我实际上是在构建一个抽象语法树解析器

This was when I realized this was a demand for a lightweight, s-expression based small language. Hence I implemented the embeddable schemyinterpreter.

那时我意识到这是对轻量级、基于 s 表达式的小型语言的需求。因此我实现了可嵌入的方案解释器。

I put an example command handling application here, which captures the essence of the design philosophy for the web server I mentioned above. It works like so:

在这里放了一个示例命令处理应用程序,它抓住了我上面提到的 Web 服务器设计理念的精髓。它的工作原理是这样的:

  1. It extends an embedded Schemy interpreter with some functions implemented in C#.

  2. It finds .ssscripts which defines a command processing pipeline by using those implemented functions.

  3. The server finds and persists the composes pipeline from a script by looking for the symbol EXECUTEwhich should be of type Func<object, object>.

  4. When a command request comes in, it simply invokes the corresponding command processor (the one defined by EXECUTE), and responses with the result.

  1. 它使用 C# 中实现的一些功能扩展了嵌入式 Schema 解释器。

  2. .ss通过使用那些实现的函数来查找定义命令处理管道的脚本。

  3. 服务器发现并持续从脚本撰写管道通过寻找符号EXECUTE应该是类型Func<object, object>

  4. 当一个命令请求进来时,它只是调用相应的命令处理器(由 定义的那个EXECUTE),并用结果进行响应。

Finally, here's a complex example script, that provides an online man-page lookup via this TCP command server:

最后,这是一个复杂的示例脚本,它通过此 TCP 命令服务器提供联机手册页查找:

; This script will be load by the server as command `man`. The command
; is consistent of the following functions chained together:
;
; 1.  An online man-page look up - it detects the current operating system and 
;     decides to use either a linux or freebsd man page web API for the look up.
; 
; 2.  A string truncator `truncate-string` - it truncates the input string, in
;     this case the output of the man-page lookup, to the specified number of
;     characters.
; 
; The client of the command server connects via raw RCP protocol, and can issue
; commands like:
; 
;     man ls
; 
; and gets response of the truncated corresponding online manpage content.

(define EXECUTE
  (let ((os (get-current-os))
        (max-length 500))
    (chain                                      ; chain functions together
      (cond                                     ; pick a manpage lookup based on OS
        ((equal? os "freebsd") (man-freebsd))
        ((equal? os "linux") (man-linux))
        (else (man-freebsd)))
      (truncate-string max-length))))           ; truncate output string to a max length

With this script loaded by the command server, a TCP client can issue commands man <unix_command>to the server:

通过命令服务器加载此脚本,TCP 客户端可以man <unix_command>向服务器发出命令:

$ ncat 127.0.0.1 8080

man ls

LS(1)                   FreeBSD General Commands Manual                  LS(1)

NAME
     ls -- list directory contents

SYNOPSIS
     ls [--libxo] [-ABCFGHILPRSTUWZabcdfghiklmnopqrstuwxy1,] [-D format]
        [file ...]

DESCRIPTION
     For each operand that names a file of a type other than directory, ls
     displays its name as well as any requested, associated information.  For
     each operand that names a file of type directory, ls displays the names
     of files contained within that directory, as well as any requested,