104

For those of you experienced in both Haskell and some flavor of Lisp, I'm curious how "pleasant" (to use a horrid term) it is to write code in Haskell vs. Lisp.

Some background: I'm learning Haskell now, having earlier worked with Scheme and CL (and a little foray into Clojure). Traditionally, you could consider me a fan of dynamic languages for the succinctness and rapidity they provide. I quickly fell in love with Lisp macros, as it gave me yet another way to avoid verbosity and boilerplate.

I'm finding Haskell incredibly interesting, as it's introducing me to ways of coding I didn't know existed. It definitely has some aspects that seem like they would aid in achieving agility, like ease of writing partial functions. However, I'm a bit concerned about losing Lisp macros (I assume I lose them; truth be told I may have just not learned about them yet?) and the static typing system.

Would anyone who has done a decent amount of coding in both worlds mind commenting on how the experiences differ, which you prefer, and if said preference is situational?

4

8 回答 8

70

简短的回答:

  • 几乎所有你可以用宏做的事情,你可以用高阶函数做(我包括单子,箭头等),但这可能需要更多的思考(但只是第一次,它很有趣,你会成为一个更好的程序员),和
  • 静态系统足够通用,它永远不会妨碍您,有点令人惊讶的是,它实际上“有助于实现敏捷性”(正如您所说),因为当您的程序编译时,您几乎可以确定这是正确的,所以这种确定性让您尝试排除你可能害怕尝试的东西——编程有一种“动态”的感觉,尽管它与 Lisp 不同。

[注意:有一个“模板 Haskell ”可以让你像在 Lisp 中一样编写宏,但严格来说你永远不需要它。]

于 2008-12-25T15:40:13.010 回答
65

First of all, don't worry about losing particular features like dynamic typing. As you're familiar with Common Lisp, a remarkably well-designed language, I assume you're aware that a language can't be reduced to its feature set. It's all about a coherent whole, isn't it?

In this regard, Haskell shines just as brightly as Common Lisp does. Its features combine to provide you with a way of programming that makes code extremely short and elegant. The lack of macros is mitigated somewhat by more elaborate (but, likewise, harder to understand and use) concepts like monads and arrows. The static type system adds to your power rather than getting in your way as it does in most object-oriented languages.

On the other hand, programming in Haskell is much less interactive than Lisp, and the tremendous amount of reflection present in languages like Lisp just doesn't fit the static view of the world that Haskell presupposes. The tool sets available to you are therefore quite different between the two languages, but hard to compare to one another.

I personally prefer the Lisp way of programming in general, as I feel it fits the way I work better. However, this doesn't mean you're bound to do so as well.

于 2008-12-25T11:28:04.470 回答
13

与 Common Lisp 相比,Haskell 中对元编程的需求更少,因为可以围绕 monad 构建很多内容,并且添加的语法使嵌入式 DSL 看起来不像树,但总是有 Template Haskell,正如ShreevatsaR所提到的,甚至是Liskell(Haskell 语义 + Lisp语法)如果你喜欢括号。

于 2009-01-07T23:01:18.263 回答
10

关于宏,这里有一个讨论它的页面:Hello Haskell,Goodbye Lisp。它解释了在 Haskell 中不需要宏的观点。它带有一个简短的示例进行比较。

需要 LISP 宏以避免评估两个参数的示例:

(defmacro doif (x y) `(if ,x ,y))

Haskell 没有系统地评估两个参数的示例情况,不需要像宏定义这样的东西:

doif x y = if x then (Just y) else Nothing

于 2010-02-23T23:20:18.270 回答
9

我是一名 Common Lisp 程序员。

前段时间尝试过 Haskell,我个人的底线是坚持使用 CL。

原因:

Haskell 确实有其自身的优点,并且以完全不同的方式做一些事情,但从长远来看,它对我来说并没有削减它。

于 2009-07-09T08:03:20.243 回答
6

在 Haskell 中,您可以定义一个 if 函数,这在 LISP 中是不可能的。这是可能的,因为惰性允许程序中的更多模块化。这篇经典论文: John Hughes 的为什么 FP 很重要,解释了惰性如何增强可组合性。

于 2009-01-12T21:10:13.593 回答
5

您可以在 Lisp 中使用 Haskell 中繁琐(如果可能的话)的宏来实现非常酷的事情。以“memoize”宏为例(参见 Peter Norvig 的 PAIP 的第 9 章)。有了它,你可以定义一个函数,比如 foo,然后简单地评估 (memoize 'foo),它将 foo 的全局定义替换为一个 memoized 版本。你能在 Haskell 中用高阶函数达到同样的效果吗?

于 2009-09-09T14:31:36.503 回答
4

当我继续我的 Haskell 学习之旅时,似乎有助于“替换”宏的一件事是能够定义自己的中缀运算符并自定义它们的优先级和关联性。有点复杂,但一个有趣的系统!

于 2008-12-29T23:42:31.847 回答