くんすとの備忘録

IT系技術メモ

移転しました。

15秒後に自動的にリダイレクトします。

【Erlang】FreeBSDにErlangを入れてみた

インストール

packageからインストール。

$ pkg_add -r erlang

インストール後のメッセージ

===========================================================================
Installation tips:

You can find an emacs mode for Erlang here:

    /usr/local/lib/erlang/lib/tools-2.6.6.5/emacs

You may wish to add the following line to /etc/manpath.config:

OPTIONAL_MANPATH        /usr/local/lib/erlang/man
===========================================================================

起動と終了

起動してみる

$ erl
Erlang R14B04 (erts-5.8.5) [source] [rq:1] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.8.5  (abort with ^G)

終了

q().

色々やってみる

erlで入って

1> 2 + 2.0.
4.0
2> "Hello World!".
"Hello World!"
3> [1, 2, 3,4].
[1,2,3,4]
4> [72,97].
"Ha"
5> [65,76,99,111,116].
"ALcot"

うむ。

7> "hoge" + 1.
** exception error: bad argument in an arithmetic expression
     in operator  +/2
        called as "hoge" + 1
8> var = 1.
** exception error: no match of right hand side value 1
9> Var = 1.
1
10> Var = 2.
** exception error: no match of right hand side value 2
11> MY_CONST = hoge.
hoge

変数は大文字始まり、代入は一回だけ。


さらにいろいろやりたくなってきたので……

Emacsを準備。

$ which emacs
emacs not found

Σ(`ロ´;)そういえばemacs入れてなかったorz

VimErlang開発環境

  • 色付けは ~/.vimrc に syntax on でおk

vim test.sh
してから、コマンドモードで
:vs study01.erl
する。

f:id:kunst1080:20130515220049j:plain

で、以下のように編集 ※画面切り替えは Ctrl+W (大文字Wだからshift押すよ)

  • study01.erl
-module(study01).
-export([another_factorial/1]).
-export([another_fib/1]).

another_factorial(0) -> 1;
another_factorial(N) -> N * another_factorial(N-1).

another_fib(0) -> 1;
another_fib(1) -> 1;
another_fib(N) -> another_fib(N-1) + another_fib(N-2).
  • test.sh
#/bin/bash
erl_run(){
    echo "erl> $1"
    erl -boot start_clean -noshell \
      -eval "io:write($1)" \
      -run init stop
    echo
}

erlc study01.erl
erl_run "study01:another_fib(1)"
erl_run "study01:another_fib(10)"
erl_run "study01:another_factorial(3)"
erl_run "study01:another_factorial(10)"


:!./test.sh で実行(chmod +x test.sh を忘れずに…)

f:id:kunst1080:20130515220104j:plain


参考にした本やサイト

ちょっと進んだ内容