Lisp事始め
Lispを始めるには?
Lispを使うにはどの実装を使えば良いのでしょうか?
CommonLisp系ではこちらにたくさん掲載されています。
CLiki: Common Lisp implementation
当然、どれを使うか迷うわけで、質問することになります。
結局のところ、Javaを多少なりとも知っているということでClojureを使ってみることにしました。
Clojure事始め(改め)
本家サイトからダウンロードができますが、Leiningenという統合ビルドツールを使うようです。もちろんClojureサイトからダウンロードしたjarでREPLができるのですが、左右の矢印キーや履歴(上矢印)ができません。そのためLeiningenから利用したほうが良いと思います。
- Leiningen
Leiningenサイトから、スクリプトを入手します。
https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
適当なディレクトリにleinというファイル名で保存し、適切(当?)なパーミッションを設定します。 スクリプトを起動すると、Leiningenのインストールが始まります。
# 入手したスクリプト (root)MacBook-Air:cloure foo$ ls -la -rw-r--r-- 1 foo staff 12713 12 29 11:29 lein # パーミッションをつける。 (root)MacBook-Air:cloure foo$ chmod 777 lein (root)MacBook-Air:cloure foo$ ls -la -rwxrwxrwx 1 foo staff 12713 12 29 11:29 lein # 初回起動でインストールされる。 (root)MacBook-Air:cloure foo$ ./lein Downloading Leiningen to /Users/foo/.lein/self-installs/leiningen-2.5.3-standalone.jar now... % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 605 0 605 0 0 259 0 --:--:-- 0:00:02 --:--:-- 259 11 15.0M 11 1801k 0 0 10049 0 0:26:09 0:03:03 0:23:06 0^C Leiningen is a tool for working with Clojure projects. Several tasks are available: change Rewrite project.clj by applying a function. check Check syntax and warn on reflection. classpath Print the classpath of the current project. clean Remove all files from project's target-path. compile Compile Clojure source into .class files. deploy Build and deploy jar to remote repository. deps Download all dependencies. do Higher-order task to perform other tasks in succession. help Display a list of tasks or help for a given task. install Install the current project to the local repository. jar Package up all the project's files into a jar file. javac Compile Java source files. new Generate project scaffolding based on a template. plugin DEPRECATED. Please use the :user profile instead. pom Write a pom.xml file to disk for Maven interoperability. release Perform :release-tasks. repl Start a repl session either with the current project or standalone. retest Run only the test namespaces which failed last time around. run Run a -main function with optional command-line arguments. search Search remote maven repositories for matching jars. show-profiles List all available profiles or display one if given an argument. test Run the project's tests. trampoline Run a task without nesting the project's JVM inside Leiningen's. uberjar Package up the project files and dependencies into a jar file. update-in Perform arbitrary transformations on your project map. upgrade Upgrade Leiningen to specified version or latest stable. vcs Interact with the version control system. version Print version for Leiningen and the current JVM. with-profile Apply the given task with the profile(s) specified. Run `lein help $TASK` for details. Global Options: -o Run a task offline. -U Run a task after forcing update of snapshots. -h, --help Print this help or help for a specific task. -v, --version Print Leiningen's version. These aliases are available: downgrade, expands to upgrade See also: readme, faq, tutorial, news, sample, profiles, deploying, gpg, mixed-source, templates, and copying.
replオプションをつけてleinを起動するとclojureをREPLで使用することができます。初回起動時は諸々モジュールがインストールされます。
(root)MacBook-Air:cloure foo$ ./lein repl Retrieving org/clojure/tools.nrepl/0.2.10/tools.nrepl-0.2.10.pom from central Retrieving org/clojure/pom.contrib/0.1.2/pom.contrib-0.1.2.pom from central Retrieving org/sonatype/oss/oss-parent/7/oss-parent-7.pom from central Retrieving clojure-complete/clojure-complete/0.2.3/clojure-complete-0.2.3.pom from clojars Retrieving org/clojure/clojure/1.7.0/clojure-1.7.0.pom from central Retrieving org/clojure/clojure/1.7.0/clojure-1.7.0.jar from central Retrieving org/clojure/tools.nrepl/0.2.10/tools.nrepl-0.2.10.jar from central Retrieving clojure-complete/clojure-complete/0.2.3/clojure-complete-0.2.3.jar from clojars nREPL server started on port 55577 on host 127.0.0.1 - nrepl://127.0.0.1:55577 REPL-y 0.3.7, nREPL 0.2.10 Clojure 1.7.0 Java HotSpot(TM) 64-Bit Server VM 1.8.0_05-b13 Docs: (doc function-name-here) (find-doc "part-of-name-here") Source: (source function-name-here) Javadoc: (javadoc java-object-or-class-here) Exit: Control+D or (exit) or (quit) Results: Stored in vars *1, *2, *3, an exception in *e
clojureのREPLを使ってみます。カッコを入力すると、瞬間的に対応するカッコにフォーカスが移動して元に戻ります。
# リストを作る。 user=> (list 'a 'b 'c) (a b c) # carは使えなようです。 user=> car (list 'a 'b 'c) CompilerException java.lang.RuntimeException: Unable to resolve symbol: car in this context, compiling:(/private/var/folders/hw/p4bp49hd7v9_1j60sjvmhqnr0000gn/T/form-init2681034788118954262.clj:1:1062) (a b c) # carではなくfirstを使います。 user=> (first (list 'a 'b 'c)) a # cdrではなくrestを使います。 user=> (rest (list 'a 'b 'c)) (b c) user=> # 足し算 user=> (+ 1 2 3 4 5) 15 # リテラルは日本語も大丈夫なようです。 user=> (list 'あ 'b) (あ b) user=>
基礎文法最速マスター
クイズ形式で学習
設問にあるアンダースコアの部分( __ )に、コードを書いて、Runボタンを押すと結果が確かめられます。
失敗した場合
成功した場合