ifとcase式でも...
というわけで、ifから再開しますー
どうやら、他の言語のifとはかなり意味合いが違うみたいです。
ここまでErlangをやってきて、しっくり来たことがない...ぬーん。先行き不安だお。
http://www.ymotongpoo.com/works/lyse-ja/ja/05_syntax_in_functions.html#if
最終的に書いたもの
- export1で指定するとどうしてもエラーになるので、最終的には、compile(export_all)で済ませた。
- if文の対象外になるとerrorになってしまうので、想定パターンで全て記述する必要がある。つまり、elseを名文化しなければならんのだー。
-module(what_the_if). %-export([heh_fine/0],[oh_god/1],[help_me/2]). % 先に別のファンクションで使った数字を後から割り当てるとエラーになる %-export([oh_god/1]). %-export([help_me/0]). -compile(export_all). heh_fine() -> if 1 =:= 1 -> works end, if 1 =:= 2; 1 =:= 1 -> works end, if 1 =:= 2, 1 =:= 1 -> fails end. oh_god(N) -> if N =:= 2 -> might_suceed; true -> always_does %% this is Erlang's if's 'else!' end. %% note, this one would be better as a pattern match in function heads! %% I'm doing it his way for the sake of the example. help_me(Animal) -> Talk = if Animal == cat -> "meow"; Animal == beef -> "mooo"; Animal == dog -> "bark"; Animal == tree -> "bark"; true -> "fgdadfgna" end, {Animal, " says " ++ Talk ++ "!"}. %% In case prepend(X,[]) -> [X]; prepend(X,Set) -> case lists:member(X,Set) of true -> Set; false -> [X|Set] end. beach(Temperature) -> case Temperature of {celsius, N} when N >= 20, N =< 45 -> 'favorable'; {kelvin, N} when N >= 293, N =< 318 -> 'scientifically favorable'; {fahrenheit, N} when N >= 68, N =<113 -> 'favorable in the US'; _ -> 'avoid beach' end.
ちなみに、最近の勉強結果はstudyplusで付けてまする。
learning you some erlangの5章のif手前までchie.hatenablog.com/entry/lyse-5-1ちょっと間...『Erlang』 1時間 0分 stdy.pl/PwsrCs #studyplus
— chie (´∀`*)さん (@crea_memo) 11月 4, 2012