【问题标题】:Scala way to write lisp-like progn?Scala 编写类似 lisp 的 progn 的方法?
【发布时间】:2014-02-02 12:37:25
【问题描述】:

Scala 中是否有类似 lisp 的 progn 之类的语言结构?
谢谢!

【问题讨论】:

  • 请添加伪 scala 代码以及您要评估的 progn 和评估结果。请注意,在 Scala 中,{a; b; c} 返回c 的结果。
  • 或者描述 progn 用自然语言做了什么。

标签: scala functional-programming lisp


【解决方案1】:

是的,花括号。

progn evaluates forms, in the order in which they are given.

The values of each form but the last are discarded.

Examples:

 (progn) =>  NIL
 (progn 1 2 3) =>  3
 (progn (values 1 2 3)) =>  1, 2, 3
 (setq a 1) =>  1
 (if a
      (progn (setq a nil) 'here)
      (progn (setq a t) 'there)) =>  HERE
 a =>  NIL

现在一样,但在 scala 中:

scala> {}

// Unit is not written explicitly, but it is here
scala> {1; 2; 3}
// warnings omitted
// res1: Int = 3

scala> {(1, 2, 3)}
// res2: (Int, Int, Int) = (1,2,3)

// no direct analog for setq, skipping other examples

并且为了确保您评估表格,按照表格给出的顺序

scala> {println('1'); println('2'); println('3')}
1
2
3

【讨论】:

  • 还有一个菜鸟问题:)PROG1,PGOG2呢?
  • 我怀疑两者都有内置机制。对于 PROG1,有一个模拟(在某种意义上)——function commonly called tap (aka kestrel combinator)
  • @om-nom-nom 两者似乎都很容易用 Scala 宏实现。
  • @ghik 是的,但我认为这不是真的需要,普通的 scala 可能性会完成这项工作
猜你喜欢
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
  • 2011-03-10
  • 2015-07-12
  • 2013-03-18
  • 2014-12-18
  • 1970-01-01
相关资源
最近更新 更多