1.6 KiB
title, author, date
| title | author | date |
|---|---|---|
| exercise 3 | fredrik robertsen | 2025-09-26 |
task 1
my implementation of the quadratic equation
a)
proc {QuadraticEquation A B C ?RealSol ?X1 ?X2} Discriminant in
Discriminant = B*B - 4.0*A*C
RealSol = Discriminant >= 0.0
if RealSol then
X1 = (~B - {Sqrt Discriminant})/(2.0*A)
X2 = (~B + {Sqrt Discriminant})/(2.0*A)
end
end
b)
this is the oz emulator output of System.showing RealSol, X1, X2
[true ~1 0.5]
[false _<optimized> _<optimized>]
c)
procedural abstractions (proc instead of fun) enable side-effects and interacting with the outside world. in this example, it is useful for providing multiple assigned return values, and because of oz's unification, not all return parameters need be assigned. as such, we are able to let X1 and X2 remain unassigned to any value, in case there are no real roots.
d)
c) already covers some of the differences. a function will have a set signature and have some different syntactic parsing. in some languages, functions don't allow sequential expressions, but oz is weird, so they are allowed. you may also print values to stdout in a function, unlike languages like haskell.
the distinction is in a way pedantic, but a useful one to make. one entails mathematical functions and all their bells and whistles, while the other corresponds more closely to how a computer works. this furthers oz as a general purpose multi-paradigm programming language.
task 2
a simple recursive sum implementation
fun {Sum List}
case List of Head|Tail then Head + {Sum Tail} else 0 end
end