From 76265730fc6453a75f2cec372ddf1b248d982ecd Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Sun, 28 Sep 2025 14:57:02 +0200 Subject: [PATCH] task 1 - quadratic formula --- assignment3/main.oz | 26 ++++++++++++++++++++++++++ assignment3/report.md | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 assignment3/main.oz create mode 100644 assignment3/report.md diff --git a/assignment3/main.oz b/assignment3/main.oz new file mode 100644 index 0000000..351ea4c --- /dev/null +++ b/assignment3/main.oz @@ -0,0 +1,26 @@ +% \insert '../assignment1/List.oz' + +declare QuadraticEquation Sum RightFold Quadratic LazyNumberGenerator TailRecursiveSum in + +% task 1 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 +% task 1 b) +local + TestQuad = proc {$ A B C} + local RealSol X1 X2 in + {QuadraticEquation A B C RealSol X1 X2} + {System.show RealSol|[X1 X2]} + end + end +in + {System.show 'testing abc'} + {TestQuad 2.0 1.0 ~1.0} + {TestQuad 2.0 1.0 2.0} +end diff --git a/assignment3/report.md b/assignment3/report.md new file mode 100644 index 0000000..8245d63 --- /dev/null +++ b/assignment3/report.md @@ -0,0 +1,41 @@ +--- +title: exercise 3 +author: fredrik robertsen +date: 2025-09-26 +--- + +## task 1 + +my implementation of the quadratic equation + +### a) + +```oz +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.show`ing `RealSol`, `X1`, `X2` + +```oz +[true ~1 0.5] +[false _ _] +``` + +### 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 `fun`ction 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.