make List importable file

This commit is contained in:
2025-09-17 12:41:03 +02:00
parent 6242a0b9d9
commit 086f2bbd78
4 changed files with 48 additions and 130 deletions

48
assignment1/List.oz Normal file
View File

@@ -0,0 +1,48 @@
functor
export
length: Length
take: Take
drop: Drop
append: Append
member: Member
position: Position
define
fun {Length List}
case List of _|Tail then (1 + {Length Tail}) else 0 end
end
fun {Take (Head|Tail) Count}
if Count > 0 then (Head|{Take Tail (Count - 1)}) else nil end
end
fun {Drop (Head|Tail) Count}
if Count > 0 then {Drop Tail (Count - 1)} else Head|Tail end
end
fun {Append List Other}
case List
of X|nil then X|Other
[] Head|Tail then Head|{Append Tail Other}
else nil end
end
fun {Member (Head|Tail) Element}
if Tail == nil
then false
else
if Head == Element
then true
else
{Member Tail Element}
end
end
end
fun {Position (Head|Tail) Element}
if Head == Element
then 0 % 0-indexed
else
1 + {Position Tail Element}
end
end
end

BIN
assignment1/List.ozf Normal file

Binary file not shown.

View File

@@ -1,130 +0,0 @@
functor
import
System
Application
define
% task 1
{System.showInfo 'hello world!'}
local X Y Z=30 in % task 3a
Y = 300
X = Y * Z
{System.show X}
end
local X Y in % task 3b
X = "This is a string"
% removing the thread block yields no print. thus, it seems the thread will
% run once all variables are bound.
% note: it also seems to run after all other sequential code has run.
thread {System.showInfo Y} end
Y = X
end
fun {Max A B} % task 4a
if A > B then A else B end
end
proc {PrintGreater A B} % task 4b
{System.showInfo {Max A B}}
end
{System.printInfo 'printgreater 1 4: '}
{PrintGreater 1 4} % -> 4
proc {Circle R} A D C Pi in % task 5
Pi = 355.0/113.0
A = Pi * R * R
D = 2.0 * R
C = Pi * D
{System.printInfo 'area: '}
{System.showInfo A}
{System.printInfo 'diameter: '}
{System.showInfo D}
{System.printInfo 'circumference: '}
{System.showInfo C}
end
{Circle 2.0}
fun {Factorial N} % task 6 - note: handles negatives as 1
if N < 2 then 1 else (N * {Factorial N - 1}) end
end
{System.printInfo 'factorial (24): '}
{System.showInfo {Factorial 4}}
fun {Length List} % task 7a
case List of Head|Tail then (1 + {Length Tail}) else 0 end
end
{System.printInfo 'length (3): '}
{System.showInfo {Length [1 2 3]}}
fun {Take (Head|Tail) Count} % task 7b
if Count > 0 then (Head|{Take Tail (Count - 1)}) else nil end
end
{System.printInfo 'take ([1 2]): '}
{System.show {Take [1 2 3 4] 2}} % showInfo doesn't print lists....
fun {Drop (Head|Tail) Count} % task 7c
if Count > 0 then {Drop Tail (Count - 1)} else Head|Tail end
end
{System.printInfo 'drop ([3 4]): '}
{System.show {Drop [1 2 3 4] 2}}
fun {Append List Other} % task 7d
case List
of X|nil then X|Other
[] Head|Tail then Head|{Append Tail Other}
else nil end
end
{System.printInfo 'append ([1 2 3 4 5 6 7]): '}
{System.show {Append [1 2 3 4] [5 6 7]}}
fun {Member (Head|Tail) Element} % task 7e
if Tail == nil
then false
else
if Head == Element
then true
else
{Member Tail Element}
end
end
end
{System.printInfo 'member (false): '}
{System.show {Member [1 2 3 4] 5}} % showInfo doesn't show booleans.
{System.printInfo 'member (true): '}
{System.show {Member [1 2 3 4] 3}}
fun {Position (Head|Tail) Element} % task 7f
if Head == Element
then 0 % 0-indexed
else
1 + {Position Tail Element}
end
end
{System.printInfo 'position (2): '}
{System.showInfo {Position [1 2 3 4] 3}}
fun {Push List Element} % task 8a
Element|List
end
{System.printInfo 'push ([0 1 2 3]): '}
{System.show {Push [1 2 3] 0}}
fun {Peek List} % task 8b
if {Length List} == 0 then nil
else List.1
end
end
{System.printInfo 'peek (1): '}
{System.show {Peek [1 2 3]}}
fun {Pop (Head|Tail)} % task 8b
Tail
end
{System.printInfo 'pop ([2 3]): '}
{System.show {Pop [1 2 3]}}
{Application.exit 0}
end

Binary file not shown.