$ hy -v
hy 0.19.0
$ hy --repl-output-fn=hy.contrib.hy-repr.hy-repr
~/.bashrc に
alias hyr="hy --repl-output-fn=hy.contrib.hy-repr.hy-repr
などとしておき
$ hyr
で起動するのがいいだろう
=> (+ 7 3)
10
=> (- 7 3)
4
=> (* 7 3)
21
=> (/ 7 3)
2.3333333333333335
=> (% 7 3)
1
=> (setv x 42)
=> x
42
リスト [‘1 ‘2 ‘3 ‘4 ‘5] と区別するため
‘(1 2 3 4 5) のことを
こちらのドキュメントでは 入れ子 と呼ぶことにする
=> (first '(1 2 3 4 5))
'1
=> (second '(1 2 3 4 5))
'2
=> (rest '(1 2 3 4 5))
<itertools.islice object at 0x...
'(2 3 4 5) 相当のものが生成されているはず
=> (last '(1 2 3 4 5))
'5
=> (cut '(1 2 3 4 5) 2)
'(3 4 5)
=> (cut '(1 2 3 4 5))
'(1 2 3 4 5)
=> (cut (rest '(1 2 3 4 5)))
TypeError: 'itertools.islice' object is not subscriptable
これはなぜかできない
=> (list '(1 2 3 4 5))
['1 '2 '3 '4 '5]
=> (list (rest '(1 2 3 4 5)))
['2 '3 '4 '5]
=> (defn check [x]
... (if (> x 0) "positive"
... (= x 0) "zero"
... "negative"))
=> (check 42)
"positive"
=> (check 0)
"zero"
=> (check -42)
"negative"
=> (defn add [x y]
... (+ x y))
=> (add 2 3)
5
=> (defmacro add [&rest args]
... `(+ ~@args))
=> (add 2 3 5)
10
; main.hy
(defn hello [name]
(+ "hello, " name))
; defmain は,プログラムの最後に置くのがいいらしい
(defmain [&rest args]
(print "hello, world")
(print args)
(print (hello (second args))))
$ hy main.hy alice bob carol
hello, world
('main.hy', 'alice', 'bob', 'carol')
hello, alice
ちなみに,hyc は,壊れているようでなにも生成してくれない
(__pycache__ 内が生成物?)
hy.lex.exceptions.PrematureEndOfInput: Premature end of input
カッコの数が合っていないので確認する
=> (setv *x* 42)
=> (defn get-*x* []
... *x*)
=> (get-*x*)
42
=> (defn set-*y* []
... (global *y*)
... (setv *y* 43))
=> (set-*y*)
=> *y*
43
=> ((fn [x y] (+ x y)) 2 3)
5
=> (defn sub [x y]
... (- x y))
=> (sub 7 3)
4
=> (sub 3 7)
-4
=> (sub :y 7 :x 3)
-4
=> (setv d {1 10 2 20 3 30})
=> (. d [2])
20
=> (setv (. d [3]) 42)
=> (print d)
{1: 10, 2: 20, 3: 42}
=> (try (. d [4])
... (except [e KeyError] (print e)))
4
(setv (. label ["text"]) "hello, world")
(assoc label "text" "hello, world")
=> (defn f [x y]
... (- x y))
=> (-> 43 (f 1))
42
=> (defn f [x y]
... (- x y))
=> (->> 1 (f 43))
42
=> (defn f [x y]
... (print (- x y))
... (- x y))
=> (as-> 59 it (f it 1)
... (f 100 it))
58
42
42
=> (py "41 + 1")
42
=> (pys "print('hoge')")
hoge
戻る
↓ 故障中