Clojure学习:运行在 Java 虚拟机(JVM)上的动态Lisp编程语言

因为HY的学习资料较少,所以通过学习Clojure来辅助学习HY。前期也看过一些Common Lisp和On Lisp的文档,现在还不太清楚HY跟谁最接近。

文档:Clojure - Getting Started

在FreeBSD下安装Clojure

在FreeBSD系统,可以直接使用pkg进行安装,先用pkg search查看安装包的名字:

复制代码
# pkg search cloj
pkg: No SRV record found for the repo 'FreeBSD'
clojure-1.11.1.1200_1          Dynamic programming language for the JVM
clojure-cider-1.15.0_1         Clojure IDE and REPL for Emacs
clojure-cider-emacs_canna-1.15.0_1 Clojure IDE and REPL for Emacs
clojure-cider-emacs_devel-1.15.0_1 Clojure IDE and REPL for Emacs
clojure-cider-emacs_devel_nox-1.15.0_1 Clojure IDE and REPL for Emacs
clojure-cider-emacs_nox-1.15.0_1 Clojure IDE and REPL for Emacs
clojure-cider-emacs_wayland-1.15.0_1 Clojure IDE and REPL for Emacs
clojure-mode.el-5.11.0_14,1    Emacs lisp module for the Clojure language
clojure-mode.el-emacs_canna-5.11.0_14,1 Emacs lisp module for the Clojure language
clojure-mode.el-emacs_devel-5.11.0_14,1 Emacs lisp module for the Clojure language
clojure-mode.el-emacs_devel_nox-5.11.0_14,1 Emacs lisp module for the Clojure language
clojure-mode.el-emacs_nox-5.11.0_14,1 Emacs lisp module for the Clojure language
clojure-mode.el-emacs_wayland-5.11.0_14,1 Emacs lisp module for the Clojure language
swank-clojure-1.2.1            Swank/SLIME support for Clojure

发现有Clojure,用pkg install 进行安装

复制代码
# pkg install clojure
Updating FreeBSD repository catalogue...
pkg: No SRV record found for the repo 'FreeBSD'
Fetching meta.conf:   0%
FreeBSD repository is up to date.
All repositories are up to date.
The following 3 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
	clojure: 1.11.1.1200_1
	rlwrap: 0.45.2_1

Installed packages to be REINSTALLED:
	liblz4-1.9.4_1,1

Number of packages to be installed: 2
Number of packages to be reinstalled: 1

The process will require 20 MiB more space.
17 MiB to be downloaded.

Proceed with this action? [y/N]: y
[1/2] Fetching rlwrap-0.45.2_1.pkg: 100%  114 KiB 116.9kB/s    00:01    
[2/2] Fetching clojure-1.11.1.1200_1.pkg: 100%   17 MiB   1.8MB/s    00:10    
Checking integrity... done (0 conflicting)
[1/3] Installing rlwrap-0.45.2_1...
[1/3] Extracting rlwrap-0.45.2_1: 100%
[2/3] Reinstalling liblz4-1.9.4_1,1...
[2/3] Extracting liblz4-1.9.4_1,1: 100%
[3/3] Installing clojure-1.11.1.1200_1...
[3/3] Extracting clojure-1.11.1.1200_1: 100%

安装好后,使用clj命令进入clojure交互界面:

复制代码
$ clj
Downloading: org/clojure/clojure/1.11.1/clojure-1.11.1.pom from central
Downloading: org/clojure/core.specs.alpha/0.2.62/core.specs.alpha-0.2.62.pom from central
Downloading: org/clojure/spec.alpha/0.3.218/spec.alpha-0.3.218.pom from central
Downloading: org/clojure/pom.contrib/1.1.0/pom.contrib-1.1.0.pom from central
Downloading: org/clojure/core.specs.alpha/0.2.62/core.specs.alpha-0.2.62.jar from central
Downloading: org/clojure/spec.alpha/0.3.218/spec.alpha-0.3.218.jar from central
Downloading: org/clojure/clojure/1.11.1/clojure-1.11.1.jar from central
Clojure 1.11.1
user=> 

学习Clojure

写一个hello world demo

Clojure 复制代码
user=> (defn hello-world [username]
  (println (format "Hello, %s" username)))

(hello-world "world")
#'user/hello-world
user=> user=> Hello, world
nil

Clojure的特点:

跟其它语言相比比较有特点的是分数,比如可以直接这样写:22/7 ,这个分数可以直接使用。

字符串可以前面加#来构成正则表达式

#"[0-9]+"

四种数据

Clojure 复制代码
'(1 2 3)     ; list
[1 2 3]      ; vector
#{1 2 3}     ; set
{:a 1, :b 2} ; map

Clojure Evaluation

Structure vs Semantics

交互模式

交互模式下×1 ×2 ×3 分别代表最近的三次返回值

  • *1 (the last result)

  • *2 (the result two expressions ago)

  • *3 (the result three expressions ago)

Clojure - Install Clojure

相关推荐
明天不下雨(牛客同名)2 小时前
为什么 ThreadLocalMap 的 key 是弱引用 value是强引用
java·jvm·算法
多多*2 小时前
Java设计模式 简单工厂模式 工厂方法模式 抽象工厂模式 模版工厂模式 模式对比
java·linux·运维·服务器·stm32·单片机·嵌入式硬件
胡图蛋.4 小时前
Spring Boot 支持哪些日志框架?推荐和默认的日志框架是哪个?
java·spring boot·后端
牛马baby4 小时前
Java高频面试之并发编程-01
java·开发语言·面试
小小大侠客5 小时前
将eclipse中的web项目导入idea
java·eclipse·intellij-idea
不再幻想,脚踏实地5 小时前
MySQL(一)
java·数据库·mysql
吃海鲜的骆驼5 小时前
SpringBoot详细教程(持续更新中...)
java·spring boot·后端
迷雾骑士5 小时前
SpringBoot中WebMvcConfigurer注册多个拦截器(addInterceptors)时的顺序问题(二)
java·spring boot·后端·interceptor
别来无恙✲5 小时前
Mybatis源码分析
java·源码分析
68岁扶墙肾透5 小时前
Java安全-FastJson反序列化分析
java·安全·web安全·网络安全·网络攻击模型·安全架构·fastjson