是否有静态类型的函数语言?
我来自静态类型背景并研究函数式编程,但我对动态类型并不完全感兴趣。我很好奇有哪些选择。
首先的一个答案是:ActionScript 3 是静态类型的,并提供了一些功能范例,尽管 AS3 程序并不经常使用它们。
I'm coming from a statically-typed background and investigating functional programming, but I'm not entirely excited about dynamic typing. I'm curious about what kind of options are out there.
One answer to start: ActionScript 3 is statically-typed and offers some functional paradigms, though AS3 programs don't often use them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有许多。我想到了 Haskell、OCaml 和 F#。如果您是一名 Windows 开发人员,F# 非常好,并且得到 Microsoft 的大力支持(以及非常强大的 StackOverflow 社区)。 Lisp 系列语言(Common Lisp、Scheme、Clojure)是动态函数语言的示例。
ActionScript 3 具有可选的静态类型。另一方面,Haskell、OCaml 和 F# 编译器使用类型推断来确定性地推断类型。当您第一次查看用 OCaml 编写的代码时,由于没有显式类型,因此它具有动态类型语言的混乱外观以及静态类型的类型安全性。我认为动态语言的可选静态类型(一个好主意)最终将被类型推断所取代,并且 10 年后静态与动态的争论将毫无意义。
There are many. Haskell, OCaml and F# come to mind. If you are a Windows developer, F# is very nice and is well supported by Microsoft (along with a very strong StackOverflow community). Lisp family languages (Common Lisp, Scheme, Clojure) are examples of dynamic functional languages.
ActionScript 3 has optional static typing. The Haskell, OCaml and F# compilers, on the other hand, use type inference to deterministically infer types. When you first look at code written in OCaml the absence of explicit types gives it the clutter free look of a dynamically typed language with the type safety of static typing. It is my opinion that optional static typing for dynamic languages (a great idea) will eventually be replaced by type inference and that 10 years from now the static versus dynamic debate will be moot.
对于像您这样的人(可能具有静态类型、面向对象、命令式背景),我的首要推荐是 Scala。 Scala 是一种命令式功能混合体,与 Java 和大多数其他语言相比,它对 OO 的支持更好。 Scala 编译为 JVM 字节码(也支持 .net,尽管使用较少)并提供非 Java JVM 语言中无与伦比的 Java 互操作性。我在我的编译器 - jgo 中使用 Scala,并且发现编写 Scala 代码非常有趣并且令人满意。学习 Scala 的最佳资源是Scala 编程,由该语言编写设计师本人。
如果您不想要混合语言,而是更喜欢直接加入 FP 的竞争,那么我会选择 Haskell。 Haskell 是一种纯粹的函数式语言;不存在可变性或效果的(一流)概念。我最喜欢的 Haskell 资源是诙谐的Learn You a Haskell for Great Good!。事实上,无论您是否打算使用 Haskell,我都强烈建议您阅读 LYAH 的一些内容,因为它是对静态类型 FP 世界的精彩介绍。我什至在考虑语言选择之前就开始阅读它。
My top recommendation for someone like you (presumably with a statically-typed, OO, imperative background) is Scala. Scala is an imperative-functional hybrid with better support for OO than Java and most other languages around. Scala compiles to JVM bytecode (.net is also supported, though used much less) and provides Java interoperability unmatched among non-Java JVM languages. I use Scala in my compiler - jgo and have found writing Scala code exceptionally fun and satisfying. The best resource for learning Scala is Programming in Scala, written by the language designer himself.
If you don't want a hybrid language and instead prefer to jump straight into the fray of FP, I'd go with Haskell. Haskell is a purely functional language; there's no (first-class) notion of mutability or effect. My favorite Haskell resource is the witty Learn You a Haskell for Great Good!. In fact, I'd heavily recommend reading some of LYAH whether or not you intend to go with Haskell, since it's a fantastic introduction to the world of statically typed FP. I'd start reading it even before thinking about language choice.
类型理论首先应用于函数式编程(类型化 lambda 演算),早于任何命令式语言。
对于最先进的静态类型系统(在命令式世界中找不到任何类似的东西)请查看 Coq 和 Agda。
The type theory was first applied to the functional programming (typed lambda calculus), long before any imperative languages.
For the most advanced static type systems (you won't find anything comparable in the imperative world) check out Coq and Agda.
Haskell 跃入我的脑海(尽管它的类型系统可能是最先进的),ML 语言家族,F# AFAIK。
haskell leaps to mind (although its type system is probably the most advanced out there), the ML family of languages, F# AFAIK.
一些具有一流词法闭包和保证消除尾部调用的静态类型语言:
没有尾调用消除:
Some statically typed languages with first-class lexical closures and guaranteed tail call elimination:
without tail call elimination:
如果您熟悉 Java 生态系统,非常类似于 Haskell 的语言 Frege 可能值得一看因为它编译为 Java 源代码。
If you are familiar with the Java ecosystem, the very Haskell-like language Frege might be worth a look as it compiles to Java source code.
惊讶地发现这里没有人提到 Kotlin 。与 scala 一样,它是 JVM 的静态类型多范式语言,对函数式编程有良好的支持。标准库确实鼓励函数式风格。 Kotlin 不仅是类型安全的,而且也是 null 安全的,并且具有出色的类型推断能力。它也比 scala 简单一些(以宏等功能为代价)并且具有更好的 Java 互操作性。
从技术上讲,你可以用 Java 进行函数式编程,但是繁重的语法、缺乏类型推断和强制异常处理对你没有多大帮助。
Surprised to find that nobody has mentioned Kotlin here. Like scala, it is a statically typed multiparadigm language for the JVM with good support for functional programming. The standard library really encourages a functional style. Not only is Kotlin type safe, it is also null safe and has great type inference. It is also a bit simpler than scala (at the expense of features like macros) and has better Java interop.
Technically, you can do functional programming in Java, but the heavy syntax, lack of type inference and forced exception handling doesn't help you much.