Lisp 风格的 Erlang - 消息传递原语

发布于 2024-08-12 11:15:58 字数 369 浏览 3 评论 0原文

我已经阅读了所有文档以及 LFE 的大部分源代码。所有演示都强调传统 lisp 角色中的基本 lisp - 一般问题解决、Hello world 和语法模拟宏。

有谁知道 LFE 如何处理消息原语?为了指定一个更精确的问题,你会如何表达这个 erlang:

A = 2,  
Pid = spawn(fun()->  
    receive  
        B when is_integer(B) -> io:format("Added: ~p~n",[A+B]);  
        _ -> nan  
    end  
end), 
Pid ! 5.  

然后,你知道,它咕哝着一些数字相加,答案是 7。

I've read through all the documentation, and most of the source of LFE. All the presentations emphasize basic lisp in traditional lisp roles - General Problem Solving, Hello world and syntax emulating macros.

Does anyone know how LFE handles messaging primitives? To specify a more precise question, how would you express this erlang:

A = 2,  
Pid = spawn(fun()->  
    receive  
        B when is_integer(B) -> io:format("Added: ~p~n",[A+B]);  
        _ -> nan  
    end  
end), 
Pid ! 5.  

And then, you know, it mumbles something about having added up some numbers and the answer being 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-08-19 11:15:58

我不是 LFE 用户,但有一个用户指南 在源代码树中。通过阅读它,我猜它是这样的:

(let ((A 2))
  (let ((Pid (spawn (lambda ()
                      (receive
                        (B (when (is_integer B))
                          (: io format "Added: ~p~n" (list (+ A B))))
                        (_ nan))))))
    (! Pid 5)))

但我很可能犯了一个错误,因为我什至没有在 LFE 中评估过它。

我的一些问题:

  • 是否有 LET* 形式或者它的行为已经像这样了?
  • 守卫是否被称为更口齿不清的 is-integer 而不是我写的 is_integer ?

I'm not an LFE user, but there is a user guide in the source tree. From reading it I would guess it is something like this:

(let ((A 2))
  (let ((Pid (spawn (lambda ()
                      (receive
                        (B (when (is_integer B))
                          (: io format "Added: ~p~n" (list (+ A B))))
                        (_ nan))))))
    (! Pid 5)))

But I'm very likely to have made a mistake since I haven't even evaluated it in LFE.

Some questions of mine:

  • Is there a LET* form or is it behaving like one already?
  • Are guards called the more lispy is-integer and not is_integer as I wrote?
撑一把青伞 2024-08-19 11:15:58

LFE 版本中严重缺乏示例,欢迎所有贡献。

克里斯蒂安的建议是正确的。我唯一的评论是变量名不需要大写,这没有错,但没有必要。

LFE let 是一个“真正的”let,其中变量绑定首先在主体中可见。您可以在 let 中使用模式。还有一个按顺序绑定的 let* 形式(实际上是宏)。

不,到目前为止,我保留了所有 Erlang 核心函数名称,就像它们在 vanilla erlang 中一样。在名称中使用 - 而不是 _ 肯定更加口齿不清,但是如何处理 OTP 中的所有其他函数名称和原子呢?一个建议是自动将 LFE 符号中的 - 映射到生成的原子中的 _,当然,然后再以另一种方式返回。这可能会起作用,但是会导致混乱吗?

然后我可以有一个如下所示的行为模块:

(defmodule foo
  (export (init 1) (handle-call 2) (handle-cast 2) (handle-info 2) ...)
  (behaviour gen-server))

(defun handle-call ...)

(defun handle-cast ...)

etc ...

但我对此非常矛盾。

There is a serious lack of examples in the LFE release, all contributions are welcome.

Christian's suggestion is correct. My only comment is that there is no need to have capitalized variable names, it is not wrong, but not necessary.

The LFE let is a "real" let in which the variable bindings are visible first in the body. You can use patterns in let. There is also a let* form (macro actually) which binds sequentially.

No, I have so far kept all the Erlang core function names just as they are in vanilla erlang. It is definitely more lispy to use -instead of _ in names, but what do you do with all the other function names and atoms in OTP? One suggestion is to automatically map - in LFE symbols to _ in the resultant atoms, and back again going the other way of course. This would probably work, but would it lead to confusion?

I could then have a behaviour module looking like:

(defmodule foo
  (export (init 1) (handle-call 2) (handle-cast 2) (handle-info 2) ...)
  (behaviour gen-server))

(defun handle-call ...)

(defun handle-cast ...)

etc ...

But I am very ambivalent about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文