Created
April 21, 2017 21:32
-
-
Save andreiglingeanu/8834aabf9c72eb48029d75a671ec800f to your computer and use it in GitHub Desktop.
An example of higher-order programming technique in Mozart lang
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
declare ShiftLeft ShiftRight FastPascal AddList GenericPascal OpList Add Xor Multiply | |
fun {GenericPascal Op N} | |
if N == 1 then [1] | |
else L in | |
L={GenericPascal Op N - 1} | |
{OpList Op {ShiftLeft L} {ShiftRight L}} | |
end | |
end | |
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 | |
fun {Add X Y} X + Y end | |
fun {Multiply X Y} X * Y end | |
fun {Xor X Y} if X == Y then 0 else 1 end end | |
fun {ShiftLeft L} | |
case L of H | T then | |
H | {ShiftLeft T} | |
else | |
[0] | |
end | |
end | |
fun {ShiftRight L} | |
0 | L | |
end | |
fun {AddList L1 L2} | |
case L1 of H1 | T1 then | |
case L2 of H2 | T2 then | |
H1 + H2 | {AddList T1 T2} | |
else | |
nil | |
end | |
else | |
nil | |
end | |
end | |
{Show {GenericPascal Xor 1}} | |
{Show {GenericPascal Xor 2}} | |
{Show {GenericPascal Xor 3}} | |
{Show {GenericPascal Add 1}} | |
{Show {GenericPascal Add 2}} | |
{Show {GenericPascal Add 3}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment