LoginSignup
8
7

More than 5 years have passed since last update.

hy

hyというプログラミング言語についてご紹介。
lisp/schemeシンタックスライクな記述形式でかける言語。
見つけただけで、あんま詳しく調べてないけど、pythonで実装されてるので興味を持った。

すんごく上辺だけど、簡単な記事を書く。

εγκατάσταση

まずはインストール。
python, pipの環境を整えている前提。

pip install hy

で終わり。

τρέξιμο

そして試しに使ってみる。

対話環境を立ち上げる

hy

幾つかコードを書いてみる。

=> (print "Hello world")
Hello world
=> (defun welcome_to_underground [name] (print (+ name ", Welcome To Underground")))
=> (welcome_to_underground "hoge")
hoge, Welcome To Underground

動いた!

Μελέτη Περίπτωσης

以下の簡易なPythonのコードをhyに書き換える場合、以下の形になる。

Python
def rating(age):
  if age <= 10:
    print("G")
  elif age <= 14:
    print("PG12")
  elif age <= 17:
    print("R15+")
  else:
    print("R18+")
hy
(defn rating [age]
   (cond
     [(<= age 10)
       (print "G")]
     [(<= age 14)
       (print "PG12")]
     [(<= age 17)
       (print "R15+")]
     [true
       (print "R18+")]))

pythonのモジュールをimportし、利用する

randomモジュールをimportし、ランダムな文字列が返るようなものを書いてみる。

random
(import random string)
(defn choice [n]
  (let [[m ""]]
      (foreach [element (range n)]
        (+= m (random.choice (+ string.ascii_letters string.digits))))
      (print m)))

実行する。

choice
=> (choice 20)
U54TuMzdTpTyWYZkc3wm
=> (choice 20)
4Al8BJXEKM9FFvAW221L

μελλοντικός

末尾再帰書こうとしたけど即時評価されてしまったし、
ちょっとアレかなと思ったものの既存のPythonライブラリを利用できるlispっぽい言語としては
面白いと思う。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7