加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 无法使用静态语言创建应用功能?

发布时间:2020-12-16 09:43:16 所属栏目:安全 来源:网络整理
导读:我用静态类型的语言(如Scala或Haskell)读过,没有办法创建或提供Lisp应用功能: (apply #'+ (list 1 2 3)) = 6 或者可能 (apply #'list '(list :foo 1 2 "bar")) = (:FOO 1 2 "bar")(apply #'nth (list 1 '(1 2 3))) = 2 这是真的吗 解决方法 静态语言中的AP
我用静态类型的语言(如Scala或Haskell)读过,没有办法创建或提供Lisp应用功能:

(apply #'+ (list 1 2 3)) => 6

或者可能

(apply #'list '(list :foo 1 2 "bar")) => (:FOO 1 2 "bar")
(apply #'nth (list 1 '(1 2 3))) => 2

这是真的吗

解决方法

静态语言中的APPLY是一个完整的APPLY。

在Lisp中APPLY将一个函数应用于参数列表。函数和参数列表都是APPLY的参数。

> APPLY可以使用任何功能。这意味着这可以是任何结果类型和任何参数类型。
> APPLY以任意和可能不同的类型以任意长度(在Common Lisp中以任意实现的常量值限制长度)。
> APPLY返回函数返回的任何类型的值作为参数。

一个类型如何检查,而不颠覆静态类型的系统?

例子:

(apply #'+ '(1 1.4))   ; the result is a float.

(apply #'open (list "/tmp/foo" :direction :input))
; the result is an I/O stream

(apply #'open (list name :direction direction))
; the result is also an I/O stream

(apply some-function some-arguments)
; the result is whatever the function bound to some-function returns

(apply (read) (read))
; neither the actual function nor the arguments are known before runtime.
; READ can return anything

互动范例:

CL-USER 49 > (apply (READ) (READ))                        ; call APPLY
open                                                      ; enter the symbol OPEN
("/tmp/foo" :direction :input :if-does-not-exist :create) ; enter a list
#<STREAM::LATIN-1-FILE-STREAM /tmp/foo>                   ; the result

现在,使用REMOVE功能的例子。我们将从不同的事物列表中删除字符a。

CL-USER 50 > (apply (READ) (READ))
remove
(#a (1 "a" #a 12.3 :foo))
(1 "a" 12.3 :FOO)

请注意,您也可以申请自己,因为申请是一个功能。

CL-USER 56 > (apply #'apply '(+ (1 2 3)))
6

还有一点轻微的复杂性,因为APPLY函数使用任意数量的参数,其中只有最后一个参数需要是一个列表:

CL-USER 57 > (apply #'open
                    "/tmp/foo1"
                    :direction
                    :input
                    '(:if-does-not-exist :create))
#<STREAM::LATIN-1-FILE-STREAM /tmp/foo1>

怎么处理呢?

>放松静态类型检查规则
>限制APPLY

上述中的一个或两个将必须以典型的静态类型检查的编程语言来完成。也不会给你一个完全静态检查和完全灵活的APPLY。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读