task 1 - quadratic formula

This commit is contained in:
2025-09-28 14:57:02 +02:00
parent 3ffa1bcffa
commit 76265730fc
2 changed files with 67 additions and 0 deletions

26
assignment3/main.oz Normal file
View File

@@ -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

41
assignment3/report.md Normal file
View File

@@ -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 _<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 `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.