为什么选择茴香?
Why Fennel?

原始链接: https://fennel-lang.org/rationale

Fennel 是一种编译到 Lua 的编程语言,它利用了 Lua 的速度快、占用空间小和可嵌入性等优点。虽然 Lua 功能强大且简单,但 Fennel 通过提供替代语法来解决 Lua 的一些不足,从而避免常见的错误。 Fennel 是一种 Lisp 方言,它使用括号语法来消除语句、运算符优先级和提前返回。它通过避免意外的全局变量和要求显式声明可重新赋值的变量来改进变量处理。Fennel 引入了更直观的表语法,使用方括号表示顺序表,使用花括号表示键值表,并分别使用 `for` 和 `each` 区分基于数字和迭代器的循环。 Fennel 为函数添加了元数检查,以及解构、模式匹配和宏系统来扩展语言,在 Lua 之上提供更现代和声明式的编程体验。现有的 Lua 库和工具与 Fennel 兼容。

这个Hacker News帖子讨论了Fennel,一种编译成Lua的Lisp语法语言。帖子里辩论了Lisp类语言的吸引力,一些人认为其过多的括号语法很繁琐,而另一些人则重视操作S-表达式的便捷性和宏的强大功能。一位评论者甚至开玩笑地反驳了这一论点,来为Lisp类语言辩护。 几位用户分享了他们使用Fennel的积极经验,尤其是在Neovim配置和游戏开发(Love2D、TIC-80、PICO-8)方面,强调了其函数式特性,例如模式匹配。一些人谨慎地指出,对于简单的配置,Fennel可能显得大材小用,并且其工具落后于Lua。人们也担忧其缺乏静态类型支持以及在沙盒Lua环境中可能出现的问题。一些用户建议使用其他类似Lisp的语言,例如Janet、LunarML、Hylang和LFE。总的来说,讨论反映了人们对Fennel优势的热情以及对其可用性和生态系统的务实考虑。
相关文章
  • 我为什么用 Lisp 编程 2025-04-11
  • (评论) 2023-11-06
  • (评论) 2025-04-11
  • (评论) 2024-06-03
  • Lisp:糖霜还是蛋糕? 2024-06-03

  • 原文
    Fennel – Why Fennel? the Fennel programming language

    Fennel is a programming language that runs on the Lua runtime.

    Why Lua?

    The Lua programming language is an excellent and very underrated tool. Is it remarkably powerful yet keeps a very small footprint both conceptually as a language and in terms of the size of its implementation. (The reference implementation consists of about nineteen thousand lines of C and compiles to 278kb.) Partly because it is so simple, Lua is also extremely fast. But the most important thing about Lua is that it's specifically designed to be put in other programs to make them reprogrammable by the end user.

    The conceptual simplicity of Lua stands in stark contrast to other "easy to learn" languages like JavaScript or Python--Lua contains very close to the minimum number of ideas needed to get the job done; only Forth and Scheme offer a comparable simplicity. When you combine this meticulous simplicity with the emphasis on making programs reprogrammable, the result is a powerful antidote to prevailing trends in technology of treating programs as black boxes out of the control of the user.

    And yet...

    So if Lua is so great, why not just use Lua? In many cases you should! But there are a handful of shortcomings in Lua which over time have shown to be error-prone or unclear. Fennel runs on Lua, and the runtime semantics of Fennel are a subset of Lua's, but you can think of Fennel as an alternate notation you can use to write Lua programs which helps you avoid common pitfalls. This allows Fennel to focus on doing one thing very well and not get dragged down with things like implementing a virtual machine, a standard library, or profilers and debuggers. Any library or tool that already works for Lua will work just as well for Fennel.

    The most obvious difference between Lua and Fennel is the parens-first syntax; Fennel belongs to the Lisp family of programming languages. You could say that this removes complexity from the grammar; the paren-based syntax is more regular and has fewer edge cases. Simply by virtue of being a lisp, Fennel removes from Lua:

    • statements (everything is an expression),
    • operator precedence (there is no ambiguity about what comes first), and
    • early returns (functions always return in tail positions).

    Variables

    One of the most common legitimate criticisms leveled at Lua is that it makes it easy to accidentally use globals, either by forgetting to add a local declaration or by making a typo. Fennel allows you to use globals in the rare case they are necessary but makes it very difficult to use them by accident.

    Fennel also removes the ability to reassign normal locals. If you declare a variable that will be reassigned, you must introduce it with var instead. This encourages cleaner code and makes it obvious at a glance when reassignment is going to happen. Note that Lua 5.4 introduced a similar idea with <const> variables, but since Fennel did not have to keep decades of existing code like Lua it was able to make the cleaner choice be the default rather than opt-in.

    Tables and Loops

    Lua's notation for tables (its data structure) feels somewhat dated. It uses curly brackets for both sequential (array-like) and key/value (dictionary-like) tables, while Fennel uses the much more familiar notation of using square brackets for sequential tables and curly brackets for key/value tables.

    In addition Lua overloads the for keyword for both numeric "count from X to Y" style loops as well as more generic iterator-based loops. Fennel uses for in the first case and introduces the each form for the latter.

    Functions

    Another common criticism of Lua is that it lacks arity checks; that is, if you call a function without enough arguments, it will simply proceed instead of indicating an error. Fennel allows you to write functions that work this way (fn) when it's needed for speed, but it also lets you write functions which check for the arguments they expect using lambda.

    Other

    If you've been programming in newer languages, you are likely to be spoiled by pervasive destructuring of data structures when binding variables, as well as by pattern matching to write more declarative conditionals. Both these are absent from Lua and included in Fennel.

    Finally Fennel includes a macro system so that you can easily extend the language to include new syntactic forms. This feature is intentionally listed last because while lisp programmers have historically made a big deal about how powerful it is, it is relatively rare to encounter situations where such a powerful construct is justified.

    For a more detailed look at the guiding principles of Fennel from a design perspective see the Values of Fennel.

    联系我们 contact @ memedata.com