LispE:具有模式编程和惰性求值的 Lisp 解释器
LispE: Lisp Interpreter with Pattern Programming and Lazy Evaluation

原始链接: https://github.com/naver/lispe

## LispE 概要 LispE 是一种紧凑的、跨平台的 Lisp 方言,旨在融合函数式编程和数组编程的优势。它建立在 Lisp 的基础简单性之上,提供了一种通用的形式主义来结合不同的编程范式。它包含一个内置编辑器(来自 NAVER 的 TAMGU 项目),并支持传统的 Lisp 运算符以及用于数字和字符串的内置类型,这些类型被视为向量以实现高效操作。 LispE 具有现代函数特性,例如组合运算符 (".") 和强大的模式匹配。它还支持数据结构和面向对象编程,具有类和方法。值得注意的是,它集成了数组运算符,并通过 Conway 的生命游戏(Game of Life)的简洁实现进行了演示。 除了核心语言特性之外,LispE 还可以用作 shell,并包含一个演示程序“minizork”,以探索其功能。它由 NAVER Corp. 以 BSD 3-Clause 许可证发布,旨在成为一个“带有所有功能的 Lisp”。

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 LispE: 带有模式编程和延迟求值的 Lisp 解释器 (github.com/naver) 4 点赞 来自 PaulHoule 1 小时前 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

Hello,

Welcome to Lisp Elémentaire, a version of Lisp that is both compact and offers a remarkable variety of functional and array language features. The code also comes with a small internal editor from another NAVER's project: TAMGU.

The main goal of LispE is to provide a multi-platform language that can harness the power of functional languages with array languages. The real strength of the Lisp language, of which LispE is a dialect, is its very simple but incredible versatile formalism that helps combining all these programming trends together in one single language.

I based a large part of this work on the following article: The Root of Lisp.

We have stashed here precompiled versions for Window and Mac OS (including M1)...

A Lisp with all the bells and whistles

LispE is a true Lisp with all the traditional operators that one can expect from such a language:

(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)

; It also provides some built-in types to handle numbers

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

; We can then add these lists together
(+ l1 l2) ; 5 7 9

; or strings

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

; We can then add these lists together
(+ s1 s2) ; ("ad" "be" "cf")

; Lists are treated as vectors

(@ s1 1) ; "b"
(@ l1 2) ; 3

; You can loop in a list
(loop i s1 (println i))

You can easily run threads

; Variables in threadspace are protected against
; value concurrencies.
(threadspace
   (seth titi 10)
   (seth toto (rho 2 10 '(0)))
)

; You declare a thread with the keyword: dethread
(dethread tst(x y)
   (threadspace
      (+= titi x)
      (set@ toto 0 y titi)
   )
)

; We launch 10 threads
(loop i (range 1 10 1)
   (tst (+ i 10) i)
)

; We wait for the threads to terminate
(wait)

; threadspace is actually a specific name space
(space thread
   (println titi) ; 145
   ; Note that the order of values is random, due to thread execution
   (println toto) ; ((0 21 46 34 60 75 108 92 126 145) (0 0 0 0 0 0 0 0 0 0))
)

Modern Functional Properties

LispE provides an alternative to parentheses with the composition operator: ".":

(sum (numbers 1 2 3)) can be written (sum . numbers 1 2 3)

LispE provides a powerfull built-in pattern matching mechanism:

; You can call a function in the pattern definition of the function
(defun checking (x y) (eq 0 (% y x)))

; the argument should be an integer, whose value is a multiple of 15
; The argument is stored in 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 provides also some interesting properties such as: Data Structures

; First we define some data structures
; nil or _ this is the same value

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

; Then some pattern methods
(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 provides also some OOP such as: Classes

; Defines a class named 'truc' with fields x, y, and z
(class truc (x y z)

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

; Defines a class named 'machin' with fields x, y, and z (with a default value of 1).

(class machin (x y (z 1))
    ; Defines a method 'appel' that takes one argument 'u'.
    (defun appel(u)
       (println 'machin)(+ x y z u)) 

    ; Defines a method 'configure' that takes one argument 'e'.
    (defun configure(e)
        (setqi x e)
        (setqi v 100))) ; add a new field: `v`

But also array language capabilities

Thanks to an internal structure implemented with arrays, we also provide some array operators.

For instance, here is how Game of life can be solved in one single instruction:

(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)

See: Array Operators

Finally, LispE can also be used as a Shell: Shell

Come and discover LispE: the Lisp Elémentaire.

We provide a fun little program to discover some of the most interesting aspects of LispE: minizork

Have a look and try it

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.
联系我们 contact @ memedata.com