ass2: do stuff
This commit is contained in:
@@ -1,48 +1,39 @@
|
||||
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
|
||||
declare Length Take Drop Append Member Position
|
||||
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 {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 {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 {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}
|
||||
fun {Member (Head|Tail) Element}
|
||||
if Tail == nil
|
||||
then false
|
||||
else
|
||||
if Head == Element
|
||||
then 0 % 0-indexed
|
||||
then true
|
||||
else
|
||||
1 + {Position Tail Element}
|
||||
{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
|
||||
39
assignment2/List.oz
Normal file
39
assignment2/List.oz
Normal file
@@ -0,0 +1,39 @@
|
||||
declare Length Take Drop Append Member Position
|
||||
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
|
||||
3
assignment2/README.md
Normal file
3
assignment2/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
## running this program
|
||||
|
||||
i switched to doing this in vscode. as such, the source code has changed a bit, and attached are my List.oz solutions from the last assignment, along with this assignments solution.
|
||||
39
assignment2/main.oz
Normal file
39
assignment2/main.oz
Normal file
@@ -0,0 +1,39 @@
|
||||
\insert './List.oz'
|
||||
|
||||
local
|
||||
fun {Lex Input}
|
||||
{String.tokens Input & }
|
||||
end
|
||||
|
||||
fun {Tokenize Lexemes}
|
||||
case Lexemes of Head|Tail then
|
||||
(case Head
|
||||
of "+" then operator(type:plus)
|
||||
[] "-" then operator(type:minus)
|
||||
[] "*" then operator(type:multiply)
|
||||
[] "/" then operator(type:divide)
|
||||
[] "p" then command(print)
|
||||
else number({String.toInt Head}) end
|
||||
)|{Tokenize Tail}
|
||||
else nil end
|
||||
end
|
||||
|
||||
fun {Interpret Tokens}
|
||||
case Tokens
|
||||
of number(A)|nil then A
|
||||
[] number(A)|number(B)|Z|Tail then case Z
|
||||
of number(C) then A|{Interpret number(B)|Z|Tail}
|
||||
[] operator(type:plus)
|
||||
then {Interpret number(A+B)|Tail}
|
||||
[] operator(type:minus)
|
||||
then {Interpret number(A-B)|Tail}
|
||||
[] operator(type:multiply)
|
||||
then {Interpret number(A*B)|Tail}
|
||||
[] operator(type:divide)
|
||||
then {Interpret number(A/B)|Tail}
|
||||
end
|
||||
else nil
|
||||
end
|
||||
end
|
||||
{Show {Interpret {Tokenize {Lex "1 2 + 3 *"}}}}
|
||||
end
|
||||
BIN
assignment2/main.ozf
Normal file
BIN
assignment2/main.ozf
Normal file
Binary file not shown.
Reference in New Issue
Block a user