Created
December 18, 2014 00:43
-
-
Save mikamix/b84c73f5c3781b8f3718 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
declare | |
fun {Fact N} | |
if N == 0 then 1 else N * {Fact N - 1} end | |
end | |
declare | |
fun {AddList L1 L2} | |
case L1 of H1 | T1 then | |
case L2 of H2 | T2 then | |
H1 + H2 | {AddList T1 T2} | |
end | |
else nil end | |
end | |
declare | |
fun {ShiftLeft L} | |
case L of H | T then | |
H | {ShiftLeft T} | |
else | |
[0] | |
end | |
end | |
declare | |
fun {ShiftRight L} | |
0 | L | |
end | |
declare | |
fun {Pascal N} | |
if N == 1 then [1] | |
else L in | |
L = {Pascal N - 1} | |
{AddList {ShiftLeft L} {ShiftRight L}} | |
end | |
end | |
declare | |
fun lazy {Ints N} | |
N | {Ints N + 1} | |
end | |
declare | |
fun lazy {PascalList Row} | |
Row | {PascalList {AddList {ShiftLeft Row} {ShiftRight Row}}} | |
end | |
declare | |
fun {PascalList2 N Row} | |
if N == 1 then [Row] | |
else | |
Row | {PascalList2 N - 1 {AddList {ShiftLeft Row} {ShiftRight Row}}} | |
end | |
end | |
declare | |
fun {OpList Op L1 L2} | |
case L1 of H1 | T1 then | |
case L2 of H2| T2 then | |
{Op H1 H2} | {OpList Op T1 T2} | |
end | |
else nil end | |
end | |
declare | |
fun {Add X Y} | |
X + Y | |
end | |
declare | |
fun {Xor X Y} | |
if X == Y then 0 else 1 end | |
end | |
declare | |
fun {GenericPascal N Op} | |
if N == 1 then [1] | |
else L in | |
L = {GenericPascal N - 1 Op} | |
{OpList Op {ShiftLeft L} {ShiftRight L}} | |
end | |
end | |
declare | |
local C in | |
C = {NewCell 0} | |
fun {Bump} | |
C := @C+1 | |
@C | |
end | |
fun {Read} | |
@C | |
end | |
end | |
declare | |
fun {NewCounter} | |
C Bump Read in | |
C = {NewCell 0} | |
fun {Bump} | |
C := @C+1 | |
@C | |
end | |
fun {Read} | |
@C | |
end | |
counter(bump:Bump read:Read) | |
end | |
declare | |
C = {NewCell 0} | |
L = {NewLock} | |
thread | |
lock L then I in | |
I = @C | |
C := I + 1 | |
end | |
end | |
thread | |
lock L then J in | |
J = @C | |
C := J + 1 | |
end | |
end | |
declare | |
fun {Comb N K} | |
{Fact N} div ({Fact K} * {Fact N - K}) | |
end | |
%%%%% exercise %%%%% | |
%%% 1.2 | |
declare | |
fun {FactUntil N K} | |
if N == K then N | |
else | |
N * {FactUntil (N - 1) K} | |
end | |
end | |
declare | |
fun {FastComb N K} | |
if K == 0 then 1 | |
else Numer Denom in | |
Numer = {FactUntil N (N - K + 1)} | |
Denom = {Fact K} | |
Numer div Denom | |
end | |
end | |
declare | |
fun {FastComb2 N K} | |
if K == 0 then 1 | |
else Numer Denom KK in | |
KK = if K > (N div 2) then N - K else K end | |
Numer = {FactUntil N (N - KK + 1)} | |
Denom = {Fact KK} | |
Numer div Denom | |
end | |
end | |
%%% 1.5 | |
declare | |
fun {SumList L} | |
case L of X|T then X + {SumList T} | |
else 0 end | |
end | |
%%% 1.6 | |
declare | |
fun {Minus X Y} X - Y end | |
declare | |
fun {Multi X Y} X * Y end | |
declare | |
fun {Multi2 X Y} (X + 1) * (Y + 1) end | |
for I in 1..10 do | |
{Show {GenericPascal I Multi2}} | |
end | |
%%% 1.7 | |
local X in | |
X = 23 | |
local X in | |
X = 24 | |
end | |
{Show X} | |
end | |
local X in | |
X = {NewCell 23} | |
X := 44 | |
{Show @X} | |
end | |
%%% 1.8 | |
declare | |
local Acc in | |
Acc = {NewCell 0} | |
fun {Accumulate N} | |
Acc := @Acc + N | |
@Acc | |
end | |
end | |
%%% 1.9 | |
declare | |
fun {Index L I} | |
case L of H | T then | |
if I == 1 then H | |
else {Index T I - 1} end | |
end | |
end | |
declare | |
fun {Insert L I V} | |
case L of H | T then | |
if I == 1 then V | T | |
else H | {Insert T I - 1 V} end | |
else | |
if I == 1 then V | nil | |
else nil | {Insert nil I - 1 V} end | |
end | |
end | |
declare | |
fun {Count L Acc} | |
case L of H | T then | |
{Count T Acc + 1} | |
else | |
Acc | |
end | |
end | |
declare | |
fun {NewStore} | |
C S Get Put Size in | |
C = {NewCounter} | |
S = {NewCell nil} | |
fun {Get I} | |
{Index @S I} | |
end | |
fun {Put I V} | |
{C.bump _} | |
S := {Insert @S I V} | |
end | |
fun {Size} | |
{C.read} | |
end | |
store(get:Get put:Put size:Size) | |
end | |
declare | |
fun {Get S I} {S.get I} end | |
declare | |
fun {Put S I V} {S.put I V} end | |
declare | |
fun {Size S} {S.size} end | |
declare | |
local S in | |
S = {NewStore} | |
{Put S 1 [1] _} | |
fun {MemoPascal N} | |
if N > {Size S} then P C in | |
P = {MemoPascal N - 1} | |
C = {AddList {ShiftLeft P} {ShiftRight P}} | |
{Put S N C _} | |
C | |
else | |
{Get S N} | |
end | |
end | |
end | |
%%% 1.10 | |
declare | |
C = {NewCell 0} | |
thread I in | |
I = @C | |
{Delay 500} | |
C := I + 1 | |
end | |
thread J in | |
J = @C | |
{Delay 500} | |
C := J + 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment