JavaScript 精简

Lobsters Hottest 工具

摘要

LispE 是 NAVER 开发的一个紧凑的 Lisp 方言,它结合了函数式和数组语言特性,并支持 PyTorch 和 llama.cpp 等 AI 库。

<p><a href="https://lobste.rs/s/lynqdm/de_bloating_javascript">评论</a></p>
查看原文
查看缓存全文

缓存时间: 2026/05/18 14:31

naver/lispe

来源:https://github.com/naver/lispe

LispE: Lisp Elémentaire

在浏览器中测试 LispE,请点击: 这里(https://naver.github.io/lispe))

欢迎来到 Lisp Elémentaire,这是一个既紧凑又提供大量函数式和数组语言特性的 Lisp 版本。该代码还附带了一个小型内部编辑器,来自 NAVER 的另一个项目:TAMGU(https://github.com/naver/tamgu)。

LispE 的主要目标是提供一种跨平台语言,能够充分利用函数式语言和数组语言的优势。 Lisp 语言(LispE 是其一个方言)的真正优势在于其非常简单但相当通用的形式化体系,有助于将所有编程趋势结合到一种单一语言中。

我的大部分工作基于以下文章:The Root of Lisp(http://www.paulgraham.com/rootsoflisp.html)。

  • 语言描述可在此处获取:Introduction to LispE(https://github.com/naver/lispe/wiki/1.-Introduction)
  • LispE 提供了大量函数,请参阅这里的索引(https://github.com/naver/lispe/wiki/Index-of-functions)。
  • 可用函数的帮助文档在这里:LispE Language Description(https://github.com/naver/lispe/wiki/5.-Description-of-Functions,-Operators-and-Libraries)
  • Wiki 索引在这里:HOME(https://github.com/naver/lispe/wiki)
  • 人工智能库:
    • lispe_torch(https://github.com/naver/lispe/tree/master/lispetorch):封装了 PyTorch 内部 libtorch 库和 SentencePiece
    • lispe_gguf(https://github.com/naver/lispe/tree/master/lispegguf):基于 llama.cpp,加载和运行 gguf 模型
    • lispe_mlx(https://github.com/naver/lispe/tree/master/lispemlx):基于 MLX 库访问 Mac GPU
    • lispe_tiktoken(https://github.com/naver/lispe/tree/master/lispetiktoken):基于 Open AI 的 tiktoken 库进行分词

在浏览器中测试 LispE

由于 LispE 是一个 WASM 库:您可以在这里(https://naver.github.io/lispe)测试 LispE。

另请参阅:基于谓词的浏览器 Agent(PREDIBAG)(https://github.com/naver/lispe/wiki/6.1.5-PREDIBAG-in-the-Browser)

查看二进制文件

我们在这里(https://github.com/naver/lispe/tree/master/binaries)存放了 Windows 和 Mac OS(包括 M1)的预编译版本…

一个功能齐全的 Lisp

LispE 是一个真正的 Lisp,具有所有传统操作符,这正是此类语言所期望的:

``Lisp (cons ’a ’(b c)) ; (a b c) (cdr ’(a b c d e)) ; ’(b c d e) (car ’(a b c d e)) ; ’a

(+ 10 20 (* 3 4)) ; 42

(list ’a ’b ’c ’d ’e) ; (a b c d e)

; 它还提供了一些内置类型来处理数字

(setq l1 (integers 1 2 3)) (setq l2 (integers 4 5 6))

; 然后我们可以将这些列表相加 (+ l1 l2) ; 5 7 9

; 或者字符串

(setq s1 (strings “a” “b” “c”)) (setq s2 (strings “d” “e” “f”))

; 然后我们可以将这些列表相加 (+ s1 s2) ; (“ad” “be” “cf”)

; 列表被视为向量

(@ s1 1) ; “b” (@ l1 2) ; 3

; 你可以遍历一个列表 (loop i s1 (println i)) ``

你可以轻松运行线程

``Lisp ; threadspace 中的变量受保护,防止值并发 (threadspace (seth titi 10) (seth toto (rho 2 10 ’(0))) )

; 使用关键字 dethread 声明一个线程 (dethread tst(x y) (threadspace (+= titi x) (set@ toto 0 y titi) ) )

; 我们启动 10 个线程 (loop i (range 1 10 1) (tst (+ i 10) i) )

; 等待线程结束 (wait)

; threadspace 实际上是一个特定的命名空间 (space thread (println titi) ; 145 ; 注意,由于线程执行,值的顺序是随机的 (println toto) ; ((0 21 46 34 60 75 108 92 126 145) (0 0 0 0 0 0 0 0 0 0)) )

``

现代函数式特性

LispE 提供了组合运算符 “.”(https://github.com/naver/lispe/wiki/5.-Description-of-Functions,-Operators-and-Libraries#composition-)作为括号的替代方案:

Lisp (sum (numbers 1 2 3)) 可以写成 (sum . numbers 1 2 3)

LispE 提供了强大的内置模式匹配(https://github.com/naver/lispe/wiki/6.1-Pattern-Functions)机制:

``Lisp ; 你可以在函数的模式定义中调用函数 (defun checking (x y) (eq 0 (% y x)))

; 参数应为整数,其值是 15 的倍数 ; 参数存储在 x 中 (defpat fizzbuzz ((integer_ (checking 15 x))) ’fizzbuzz) (defpat fizzbuzz ((integer_ (checking 3 x))) ’fizz) (defpat fizzbuzz ((integer_ (checking 5 x))) ’buzz) (defpat fizzbuzz (x) x) (mapcar ’fizzbuzz (range 1 100 1)) ``

LispE 还提供了一些有趣的特性,例如:数据结构(https://github.com/naver/lispe/wiki/6.7-Data-Structures)

``Lisp ; 首先我们定义一些数据结构 ; nil 或 _ 是相同的值

(data@ (Point integer_ integer_) (Pixel _ _) (Circle (Point _ _) _) (Rectangle (Point _ _) _ nil))

; 然后是一些模式方法 (defpat Surface ((Circle (Point x y) r)) (* _pi r r)) (defpat Surface ((Rectangle _ h w)) (* h w)) (defpat Surface (true) ’wrong)

(println “Circle:” (Surface (Circle (Point 12 32) 10))) (println “Rectangle:” (Surface (Rectangle (Point 21 20) 10 20)))

(setq x 10) (setq y 20) (setq r 12)

(setq cercle (Circle (Point x y) r))

(println “Circle:” (Surface cercle)) (println “Point:” (Surface (Point 10 20))) (println “Circle:” (Surface ((atom “Circle”) (Point 20 30) 3)))

(data@ Shape (Triangle _) (Square _))

(defpat dimension ( (Shape x)) (println ’Dimension x) ) (dimension (Triangle 10)) (dimension (Square 20)) ``

LispE 还提供了一些面向对象编程特性,例如:类(https://github.com/naver/lispe/wiki/6.21-Classes)

``Lisp ; 定义一个名为 ‘truc’ 的类,包含字段 x、y 和 z (class@ truc (x y z)

(defun appel(u) (setq x 100) (println x y z) (+ x y z u) ) )

; 定义一个名为 ‘machin’ 的类,包含字段 x、y 和 z(默认值为 1) (class@ machin (x y (z 1)) ; 定义一个方法 ‘appel’,接受一个参数 ‘u’ (defun appel(u) (println ’machin)(+ x y z u))

; 定义一个方法 'configure',接受一个参数 'e'
(defun configure(e)
    (setqi x e)
    (setqi v 100))) ; 添加一个新字段:`v`

``

但也具备数组语言能力

得益于使用数组实现的内部结构(https://github.com/naver/lispe/wiki/2.3-Lists),我们还提供了一些数组操作符。

例如,这里是如何用一个指令解决 生命游戏(https://github.com/naver/lispe/wiki/6.20-Conway-Game-of-Life-in-LispE):

``Lisp (defmacro ⊖(l n) (rotate l n true))

(defun lifeinit(x y) (rho x y . random_choice (* x y) (integers 0 1) 17))

(setq r (lifeinit 20 40)) (defun gol8(⍵) ((λ(⍺) (| (& (== ⍺ 4) r) (== ⍺ 3))) (⌿ ’+ (⌿ ’+ (° (λ (x ⍺) (⊖ ⍺ x)) ’(1 0 -1) (↑ (λ (x) (⌽ ⍵ x)) ’(1 0 -1)))))))

(println . prettify (gol8 r) 20) ``

请参阅:数组操作符(https://github.com/naver/lispe/wiki/5.3-A-la-APL)

动手尝试

最后,LispE 还可以用作 Shell:Shell(https://github.com/naver/lispe/wiki/7.-Shell)

来发现 LispE 吧:Lisp Elémentaire

我们提供了一个有趣的小程序,用于探索 LispE 最有趣的方面:minizork(https://github.com/naver/lispe/blob/master/examples/patterns/minizork_en.lisp)

看一看并试试吧

许可证

BSD 3-Clause License

`` LispE Copyright (c) 2020-present NAVER Corp.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ``

相似文章

将 Python 转译为 Lisp

Lobsters Hottest

LispE 是 NAVER 推出的一款开源 Lisp 方言,兼具函数式与数组编程特性,并支持 PyTorch、llama.cpp 以及 MLX 等 AI 库。该语言既可作为原生应用运行,也可打包为支持多线程与现代函数式编程特性的 WebAssembly 库。

Lisp在网页应用中的运用(2001)

Hacker News Top

Paul Graham结合自己创办Viaweb的经验,讨论了在网页应用中使用Lisp的优势,包括语言自由度、增量开发以及快速修复bug。

Vim中的Lisp(2019)

Hacker News Top

详细比较了Slimv和Vlime这两个用于交互式Lisp编程的Vim插件,涵盖安装、功能及推荐。