Skip to content

Instantly share code, notes, and snippets.

@inoshiro
Created January 12, 2012 14:38
Show Gist options
  • Save inoshiro/1600884 to your computer and use it in GitHub Desktop.
Save inoshiro/1600884 to your computer and use it in GitHub Desktop.
7つの言語 7つの世界 のセルフスタディで書いたコード。
-module(day01_count).
-export([count_words/1]).
-export([count_ten/0]).
count_words(Str) ->
count_words(Str, 1).
count_words([], Num) ->
Num;
count_words(Str, Num) ->
[Head|Tail] = Str,
case Head of
32 -> count_words(Tail, Num + 1);
_ -> count_words(Tail, Num)
end.
count_ten() ->
count_ten(1).
count_ten(10) ->
10;
count_ten(Num) ->
io:write(Num),
io:nl(),
count_ten(Num + 1).
-module(day01_errormsg).
-export([get_message/1]).
get_message(success) -> io:put_chars("success"), io:nl();
get_message({error, Message}) -> io:put_chars("error: " ++ Message), io:nl().
1> Calc = fun({Item, Quantity, Price}) -> {Item, Quantity * Price} end.
#Fun<erl_eval.6.80247286>
2> Calc({"MacBookAir", 110000, 100}).
{"MacBookAir",11000000}
3> Items = [{"MacBookAir", 110000, 5}, {"iPhone", 50000, 10}, {"iPad", 70000, 3}].
[{"MacBookAir",110000,5},
{"iPhone",50000,10},
{"iPad",70000,3}]
4> [Calc(Item) || Item <- Items].
[{"MacBookAir",550000},{"iPhone",500000},{"iPad",210000}]
-module(day02_listutil).
-export([get_key/2]).
get_key(Items, Key) ->
Check = fun({K,_}) -> K == Key end,
[{_, V}] = lists:filter(Check, Items),
V.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment