一、HY语言简介
HY语言的起源与发展
HY语言是一个基于Lisp的方言,它的语法更接近Python,但保持了Lisp的基本结构和特性。HY语言由Paul Tagliamonte等人在Python社区中开发,旨在将Lisp的灵活性和Python的易用性结合起来。
HY语言的特点与优势
HY语言具有以下特点和优势:
- 宏(Macros):HY支持Lisp风格的宏系统,允许开发者在编译时扩展语言本身。
- 动态类型:HY是动态类型语言,无需显式声明变量类型。
- 简洁易读:HY的语法接近Python,易于学习和理解。
- 扩展性强:HY可以与Python库无缝集成,使用Python的丰富生态系统。
HY语言的应用场景
HY语言适用于各种应用场景,包括:
- Web开发:使用HY编写Web应用程序的后端逻辑。
- 数据分析:利用HY的宏和动态类型特性进行数据分析和可视化。
- 脚本编写:编写自动化脚本以简化日常任务。
- 学术研究:HY的灵活性和扩展性使其成为学术研究的有力工具。
二、 HY语言安装与环境配置
安装HY
要使用HY语言,只需要在python环境中pip安装即可
pip install hy
使用HY
安装好后,在命令键入hy,即可进入hy的交互界面:
~/work/lisp]$ hy
Hy 0.29.0 using CPython(main) 3.10.13 on FreeBSD
=> (print "Hello World")
Hello World
一个简单的“Hello, HY!”程序示例:
(print "Hello World")
HY语法简单,比如上面的Hello World输出,也只需要print一下即可。当然HY跟其它Lisp语言一样,括号非常多,这是Lisp独家特色。
也可以将print语句放入main函数,然后调用main函数:
(defn main []
(print "Hello, HY!"))
(main)
除了在交互界面执行,也可以在命令行中进入HY文件的目录,然后运行hy filename.hy
(将filename.hy
替换为你的文件名)即可执行程序。然后就会在命令行中看到“Hello, HY!”的输出。
三、HY与python的异同
括号前缀语法
Hy 的第一件事是,它使用了 Lisp 传统的括号前缀语法,而不是 Python 的类似 C 的中缀语法。比如3+2 ,在HY中写作(+ 3 2):
下面是一些常见实现的对比
Hy | Python |
---|---|
(setv foobar (+ 2 2)) (setv [tim eric] ["jim" "derrick"]) (setv alpha "a" beta "b") | foobar = 2 + 2 tim, eric = 'jim', 'derrick' alpha = 'a'; beta = 'b' |
(sorted "abcBC" :key (fn [x] (.lower x))) | sorted("abcBC", key = lambda x: x.lower()) |
(defn test [a b [c "x"] #* d] [a b c d]) | def test(a, b, c="x", *d): return [a, b, c, d] |
(with [o (open "file.txt" "rt")] (setv buffer []) (while (< (len buffer) 10) (.append buffer (next o)))) | with open('file.txt', 'rt') as o: buffer = [] while len(buffer) < 10: buffer.append(next(o)) |
(lfor x (range 3) y (range 3) :if (= (+ x y) 3) (* x y)) | [x * y for x in range(3) for y in range(3) if x + y == 3] |
(defmacro do-while [test #* body] `(do ~@body (while ~test ~@body))) (setv x 0) (do-while x (print "Printed once.")) | x = 0 print("Printed once.") while x: print("Printed once.") |
另外一些区别:混合表达式
Hy 还消除了 Python 对混合表达式和语句的限制,允许更直接和功能化的代码。比如with,python无法返回值,所以是采用赋给变量的形式将数据传出来。我们可以在当前目录写foo和bar两个文件,文件内容随便,然后python执行看看:
with open("foo") as o:
f1 = o.read()
with open("bar") as o:
f2 = o.read()
print(len(f1) + len(f2))
python withpython.py
15
HY是可以直接使用with的返回值的
(print (sum (gfor
filename ["foo" "bar"]
(len (with [o (open filename)] (.read o))))))
=> (print (sum (gfor
... filename ["foo" "bar"]
... (len (with [o (open filename)] (.read o))))))
15
Hy 为 Python 的二进制运算符提供了几种概括。可以为运算符提供两个以上的参数(例如, (+ 1 2 3)
),包括增强赋值运算符(例如, (+= x 1 2 3)
)。它们也作为同名的普通一类函数提供,允许将它们传递给高阶函数: (sum xs)
可以从模块 hy.pyops
导入函数 +
后编写 (reduce + xs)
。
四、HY与其它Lisp语言的异同
在运行时,Hy 本质上是 Python 代码。因此,虽然 Hy 的设计很大程度上归功于 Clojure,但它与 Python 的耦合比 Clojure 与 Java 的耦合更紧密;一个更好的类比是 CoffeeScript 与 JavaScript 的关系。Python 的内置函数和数据结构可直接使用:
=> (print (int "deadbeef" :base 16)) ; 3735928559
3735928559
=> (print (len [1 10 100])) ; 3
3
来自 PyPI 和其他地方的第三方 Python 库也是如此。这是 Hy 中的一个小型 CherryPy Web 应用程序:
(import cherrypy)
(defclass HelloWorld []
(defn [cherrypy.expose] index [self]
"Hello World!"))
(cherrypy.quickstart (HelloWorld))
当然,如果python环境没有安装cherrypy,还需要使用pip安装:
pip install cherrypy
web服务启动后,log如下:
=> (cherrypy.quickstart (HelloWorld))
[20/Jun/2024:23:14:18] ENGINE Listening for SIGTERM.
[20/Jun/2024:23:14:18] ENGINE Listening for SIGHUP.
[20/Jun/2024:23:14:18] ENGINE Listening for SIGUSR1.
[20/Jun/2024:23:14:18] ENGINE Bus STARTING
CherryPy Checker:
The Application mounted at '' has an empty config.
[20/Jun/2024:23:14:18] ENGINE Started monitor thread 'Autoreloader'.
[20/Jun/2024:23:14:18] ENGINE Serving on http://127.0.0.1:8080
[20/Jun/2024:23:14:18] ENGINE Bus STARTED
127.0.0.1 - - [20/Jun/2024:23:14:35] "GET / HTTP/1.1" 200 12 "" "Mozilla/5.0 (X11; FreeBSD amd64; rv:122.0) Gecko/20100101 Firefox/122.0"
127.0.0.1 - - [20/Jun/2024:23:14:36] "GET /favicon.ico HTTP/1.1" 200 1406 "http://127.0.0.1:8080/" "Mozilla/5.0 (X11; FreeBSD amd64; rv:122.0) Gecko/20100101 Firefox/122.0"
总结:
HY可以同时给多个变量赋值
=> (setv x 1 y 2)
=> [x y]
[1 2]
=> (setv [x y] [1 2])
=> [x y]
[1 2]
HY中单引号’是quota ,所以不能成对使用,需要字符串等的时候,可以用单个quota,也可以使用两个“双引号”。
=> "hello"
"hello"
=> 'hello
'hello
=> (print "hello")
hello
=> (print 'hello)
hello
下一章我们通过python学HY:HY编程快速入门实践课第二章 跟着python学HY-CSDN博客