-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
HpyHacking
committed
May 26, 2011
0 parents
commit 814618f
Showing
8 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.beam | ||
.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]). | ||
|