





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Information about homework 4 for cop 4020 - programming languages 1 course at carnegie mellon university. The homework focuses on declarative concurrency and basic techniques of declarative concurrent programming using stream programming and lazy functional programming.
Typology: Assignments
1 / 9
This page cannot be seen from the preview
Don't miss anything!






Thread and Dataflow Behavior Semantics
Streams and Lazy Functional Programming
% $Id: FloatPredicates.oz,v 1.3 2007/10/23 02:14:21 leavens Exp leavens $ %% Some functions to do approximate equality of floating point numbers. %% AUTHOR: Gary T. Leavens
declare %% Return true iff the difference between X and Y %% is no larger than Epsilon fun {Within Epsilon X Y} {Abs X-Y} =< Epsilon end
%% Partly curried version of Within fun {WithinMaker Epsilon} fun {$ X Y} {Within Epsilon X Y} end end
%% Return true iff the corresponding lists are %% equal relative to the given predicate fun {CompareLists Pred Xs Ys} case Xs#Ys of nil#nil then true [] (X|Xr)#(Y|Yr) then {Pred X Y} andthen {CompareLists Pred Xr Yr} else false end end
%% Return true iff the lists are equal %% in the sense that the corresponging elements %% are equal to within Epsilon fun {WithinLists Epsilon Xs Ys} {CompareLists {WithinMaker Epsilon} Xs Ys} end
%% Return true iff the ratio of X-Y to Y is within Epsilon fun {Relative Epsilon X Y} {Abs X-Y} =< Epsilon*{Abs Y} end
%% Partly curried version of Relative fun {RelativeMaker Epsilon} fun {$ X Y} {Relative Epsilon X Y} end end
%% Return true iff the lists are equal %% in the sense that the corresponging elements %% are relatively equal to within Epsilon fun {RelativeLists Epsilon Xs Ys} {CompareLists {RelativeMaker Epsilon} Xs Ys} end
%% A useful tolerance for testing StandardTolerance = 1.0e~
%% A convenience for testing, relative equality with a fixed Epsilon ApproxEqual = {RelativeMaker StandardTolerance}
% $Id: FloatTesting.oz,v 1.4 2006/10/27 08:57:49 leavens Exp $ % Testing for floating point numbers. % AUTHOR: Gary T. Leavens
\insert ’FloatPredicates.oz’ \insert ’TestingNoStop.oz’
declare %% TestMaker returns a procedure P such that {P Actual ’=’ Expected} %% is true if {FloatCompare Epsilon Actual Expected} (for Floats) %% or if {FloatListCompare Epsilon Actual Expected} (for lists of Floats) %% If so, print a message, otherwise throw an exception. fun {TestMaker FloatCompare FloatListCompare Epsilon} fun {Compare Actual Expected} if {IsFloat Actual} andthen {IsFloat Expected} then {FloatCompare Epsilon Actual Expected} elseif {IsList Actual} andthen {IsList Expected} then {FloatListCompare Epsilon Actual Expected} else false end end in proc {$ Actual Connective Expected} if {Compare Actual Expected} then {System.showInfo {Value.toVirtualString Actual 5 20}
else {System.showInfo ’TEST FAILURE: ’
} end end end
WithinTest = {TestMaker Within WithinLists StandardTolerance} RelativeTest = {TestMaker Relative RelativeLists StandardTolerance}
% $Id: EasyDiff.oz,v 1.3 2007/10/31 00:44:27 leavens Exp leavens $ declare fun {EasyDiff F X Delta} ({F (X+Delta)} - {F X}) / Delta end
% $Id: SquareRootTest.oz,v 1.2 2007/10/31 00:35:49 leavens Exp leavens $ \insert ’SquareRoot.oz’ \insert ’FloatTesting.oz’ {StartTesting ’SquareRoot...’} Millionth = 0. WithinMillionth = {TestMaker Within WithinLists Millionth} {WithinMillionth {SquareRoot 1.0 Millionth 2.0} ’~=~’ 1.41421356} {WithinMillionth {SquareRoot 1.0 Millionth 4.0} ’~=~’ 2.0} {WithinMillionth {SquareRoot 1.0 Millionth 64.0} ’~=~’ 8.0} {WithinMillionth {SquareRoot 1.0 Millionth 3.14159} ’~=~’ 1.7724531}
% $Id: RelativeSquareRootTest.oz,v 1.2 2007/10/31 00:36:50 leavens Exp leavens $ \insert ’RelativeSquareRoot.oz’ \insert ’FloatTesting.oz’ {StartTesting ’RelativeSquareRoot...’} TenThousandth = 0. RelativeTenThousandth = {TestMaker Relative RelativeLists TenThousandth} {RelativeTenThousandth {RelativeSquareRoot 1.0 TenThousandth 2.0} ’~=~’ 1.41421356} {RelativeTenThousandth {RelativeSquareRoot 1.0 TenThousandth 4.0} ’~=~’ 2.0} {RelativeTenThousandth {RelativeSquareRoot 1.0 TenThousandth 64.0} ’~=~’ 8.0} {RelativeTenThousandth {RelativeSquareRoot 1.0 TenThousandth 3.14159} ’~=~’ 1.7724531} {RelativeTenThousandth {RelativeSquareRoot 1.0 TenThousandth 9.0e24} ’~=~’ 3.0e12} {RelativeTenThousandth {RelativeSquareRoot 1.0 TenThousandth 9.0e~40} ’~=~’ 3.0e~20}
% $Id: DiffApproximsTest.oz,v 1.2 2007/10/31 00:45:11 leavens Exp leavens $ \insert ’DiffApproxims.oz’ \insert ’FloatTesting.oz’ {StartTesting ’DiffApproxims...’} {WithinTest {List.take {DiffApproxims 500.0 fun {$ X} XX end 20.0} 9} ’~=~’ [540.0 290.0 165.0 102.5 71.25 55.625 47.8125 43.9062 41.9531]} {WithinTest {List.take {DiffApproxims 100.0 fun {$ X} XX*X end 10.0} 8} ’~=~’ [13300.0 4300.0 1675.0 831.25 526.562 403.516 349.316 324.048]}