Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
HpyHacking committed May 26, 2011
0 parents commit 814618f
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.beam
.*
12 changes: 12 additions & 0 deletions chapter_1/boolean.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-module(boolean).
-export([b_not/1, b_and/2, b_or/2]).%, b_nand/2]).

b_not(true) -> false;
b_not(false) -> true.

b_and(true, true) -> true;
b_and(_, _) -> false.

b_or(true, _) -> true;
b_or(_, true) -> true;
b_or(_, _) -> false.
8 changes: 8 additions & 0 deletions chapter_1/demo.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-module(demo).
-export([double/1, area/1]).

double(Value) -> times(Value, 2).
times(X, Y) -> X * Y.

area(sharp) -> {sharp, 1};
area(Other) -> {sharp, Other}.
19 changes: 19 additions & 0 deletions chapter_2/db.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-module(db).
-export([new/0, destroy/1, write/3, delete/2, read/2, match/2]).

new() -> [].
destroy(_Db) -> successful.

write(Key, Val, Db) -> [{Key, Val} | Db].

delete(_Key, []) -> [];
delete(Key, [{HK, _}|T]) when Key == HK -> delete(Key, T);
delete(Key, [H|T]) -> [H|delete(Key, T)].

read(_Key, []) -> {error, instance};
read(Key, [{H_Key, Val}|_T]) when Key == H_Key -> {ok, Val};
read(Key, [_H|T]) -> read(Key, T).

match(_Val, []) -> [];
match(Val, [{H_Key, H_Val} | T]) when H_Val == Val -> [H_Key | match(Val, T)];
match(Val, [_H|T]) -> match(Val, T).
27 changes: 27 additions & 0 deletions chapter_2/every.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-module(every).
-export([add_one/1, avg/1, even/1, member/2, sum_acc/2, bump/2]).

add_one([]) -> [];
add_one([H|T]) -> [H + 1 | add_one(T)].

%bump([], List) -> reverse(List);
bump([H|T], List) -> bump(T, [H+1|List]).

avg(List) -> sum(List) / len(List).

sum([]) -> 0;
sum([H|T]) -> H + sum(T).

len([]) -> 0;
len([_|T]) -> 1 + len(T).

even([]) -> [];
even([Head | Tail]) when Head rem 2 == 0 -> [Head | even(Tail)];
even([_ | Tail]) -> even(Tail).

member(_, []) -> false;
member(Item, [H|_]) when Item == H -> true;
member(Item, [_|T]) -> member(Item, T).

sum_acc([], Sum) -> Sum;
sum_acc([H|T], Sum) -> sum_acc(T, Sum + H).
10 changes: 10 additions & 0 deletions chapter_2/example.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-module(example).
-export([even/1, number/1]).

even(Int) when Int rem 2 == 0 -> true;
even(Int) when Int rem 2 == 1 -> false.

number(Num) when is_integer(Num) -> interger;
number(Num) when is_float(Num) -> float;
number(_Other) -> false.

14 changes: 14 additions & 0 deletions chapter_2/exec.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-module(exec).
-export([sum/1, sum/2, create/1]).

% 练习3-1 表达式求值
sum(1) -> 1;
sum(I) when I > 0 -> I + sum(I - 1);
sum(_) -> {error, "less one"}.

sum(N, M) when N > M -> exit("N > M");
sum(N, N) -> N;
sum(N, M) -> N + sum(N + 1, M).

create(0) -> [];
create(C) when C > 0 -> [C | create(C - 1)].
17 changes: 17 additions & 0 deletions chapter_2/list.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-module(list).
-compile(export_all).

filter([], _N) -> [];
filter([H|T], N) when H =< N -> [H | filter(T, N)];
filter([_H|T], N) -> filter(T, N).

reverse(List) -> reverse(List, []).

% Good Job 要多使用尾递归
reverse([], R) -> R;
reverse([H|T], R) -> reverse(T, [H|R]).

concatenate([], R) -> reverse(R);
concatenate([[]|Ty], R) -> concatenate(Ty, R);
concatenate([[H|Tx]|Ty], R) -> concatenate([Tx|Ty], [H|R]).

0 comments on commit 814618f

Please sign in to comment.