105 lines
2.3 KiB
Plaintext
105 lines
2.3 KiB
Plaintext
functor
|
|
import
|
|
System
|
|
Application
|
|
define
|
|
% Task 1
|
|
proc {QuadraticEquation A B C ?RealSol ?X1 ?X2}
|
|
local S = B*B + ~4.0*A*C in
|
|
if S >= 0.0 then
|
|
RealSol = true
|
|
X1 = (~B + {Float.sqrt S})/(2.0 * A)
|
|
X2 = (B + {Float.sqrt S})/(2.0 * A)
|
|
else
|
|
RealSol = false
|
|
end
|
|
end
|
|
end
|
|
|
|
local RealSol X1 X2 in
|
|
{QuadraticEquation 2.0 1.0 ~1.0 RealSol X1 X2}
|
|
if RealSol then
|
|
{System.showInfo "Got real solution"}
|
|
{System.showInfo "X1: "#X1}
|
|
{System.showInfo "X2: "#X2}
|
|
else
|
|
{System.showInfo "No real solution"}
|
|
end
|
|
end
|
|
local RealSol X1 X2 in
|
|
{QuadraticEquation 2.0 1.0 2.0 RealSol X1 X2}
|
|
if RealSol then
|
|
{System.showInfo "Got real solution"}
|
|
{System.showInfo "X1: "#X1}
|
|
{System.showInfo "X2: "#X2}
|
|
else
|
|
{System.showInfo "No real solution"}
|
|
end
|
|
end
|
|
|
|
% Task 2
|
|
fun {Sum List}
|
|
if List == nil then
|
|
0
|
|
else
|
|
List.1 + {Sum List.2}
|
|
end
|
|
end
|
|
|
|
{System.showInfo "Sum of list: "# {Sum [2 3 5 7]}}
|
|
|
|
% Task 3
|
|
fun {RightFold List Op U}
|
|
if List == nil then
|
|
U
|
|
else
|
|
{Op List.1 {RightFold List.2 Op U}}
|
|
end
|
|
end
|
|
|
|
{System.showInfo "Mul of list: "# {RightFold [2 3 5] fun {$ X Y} X * Y end 1}}
|
|
|
|
fun {NewSum List}
|
|
{RightFold List fun {$ X Y} X + Y end 0}
|
|
end
|
|
fun {NewLength List}
|
|
{RightFold List fun {$ X Y} 1 + Y end 0}
|
|
end
|
|
{System.showInfo "New Sum of list: "# {NewSum [2 3 5 7 11]}}
|
|
{System.showInfo "New Length of list: "# {NewLength [2 3 5 7 11]}}
|
|
|
|
% Task 4
|
|
fun {Quadratic A B C}
|
|
fun {$ X} A*X*X + B*X + C end
|
|
end
|
|
{System.showInfo {{Quadratic 3 2 1} 2}}
|
|
|
|
% Task 5
|
|
fun {LazyNumberGenerator X}
|
|
X|fun {$} {LazyNumberGenerator X + 1} end
|
|
end
|
|
{System.showInfo {LazyNumberGenerator 0}.1}
|
|
{System.showInfo {{LazyNumberGenerator 0}.2}.1}
|
|
{System.showInfo {{{{{{LazyNumberGenerator 0}.2}.2}.2}.2}.2}.1}
|
|
|
|
fun {TailRecursiveSumInternal List X}
|
|
if List == nil then X else
|
|
{TailRecursiveSumInternal List.2 X + List.1}
|
|
end
|
|
end
|
|
fun {TailRecursiveSum List}
|
|
{TailRecursiveSumInternal List 0}
|
|
end
|
|
|
|
fun {TailRecursiveLengthInternal List X}
|
|
if List == nil then X else
|
|
{TailRecursiveLengthInternal List.2 X + 1}
|
|
end
|
|
end
|
|
fun {TailRecursiveLength List}
|
|
{TailRecursiveLengthInternal List 0}
|
|
end
|
|
{System.showInfo "Tail Recursive Sum of list: "# {TailRecursiveSum [2 3 5 7 11]}}
|
|
{System.showInfo "Tail Recursive Length of list: "# {TailRecursiveLength [2 3 5 7 11]}}
|
|
end
|