(define cartesian-product
[ ] _ -> [ ]
[X | Y] Z -> (append (map (/. E [X E]) Z) (cartesian-product Y Z)))
(define powerset
[] -> [[]]
[X | Y] -> (let Powerset (powerset Y)
Sets (map (/. Z [X | Z]) Powerset)
(append Sets Powerset)))
(define bubble-sort
R X -> (bubble-again-perhaps R (bubble R X) X))
(define bubble
_ [ ] -> [ ]
_ [X] -> [X]
R [X Y | Z] -> [Y | (bubble R [X | Z])] where (R Y X)
R [X Y | Z] -> [X | (bubble R [Y | Z])])
(define bubble-again-perhaps
_ X X -> X
R X _ -> (bubble-sort R X))
(define newtons-method
N -> (let Guess (/ N 2.0)
(run-newtons-method N (average Guess (/ N Guess)) Guess)))
(define run-newtons-method
_ Better_Guess Guess -> Better_Guess where (close-enough? Better_Guess Guess)
N Better_Guess _
-> (run-newtons-method N
(average Better_Guess (/ N Better_Guess))
Better_Guess))
(define average
M N -> (/ (+ M N) 2.0))
(define close-enough?
Better_Guess Guess -> (< (abs (- Better_Guess Guess)) .001))
(define abs
N -> (if (> 0 N) (- 0 N) N))
(define converge
F X R -> (converge-help F (F X) X R))
(define converge-help
_ New Old R -> New where (R New Old)
F New _ R -> (converge-help F (F New) New R))
(define bubble-sort
R X -> (converge (/. Y (bubble R Y)) X (function =)))
(define newtons-method
N -> (converge (/. M (average M (/ N M)))
(/ N 2.0)
(function close-enough?)))
(define assess-spreadsheet
Spreadsheet -> (map (/. Row (assign-fixed-values Row Spreadsheet)) Spreadsheet))
(define assign-fixed-values
[Index | Cells] Spreadsheet
-> [Index | (map (/. Cell (assign-cell-value Cell Spreadsheet)) Cells)])
(define assign-cell-value
[Attribute Value] _ -> [Attribute Value] where (fixed-value? Value)
[Attribute Value] Spreadsheet -> [Attribute (Value Spreadsheet)])
(define fixed-value?
Value -> (or (number? Value) (symbol? Value) (string? Value)))
(define get-val
Index Attribute Spreadsheet -> (get-row Index Attribute Spreadsheet Spreadsheet))
(define get-row
\\ looks for the right row using the index
Index Attribute [[Index | Cells] | _] Spreadsheet
-> (get-cell Attribute Cells Spreadsheet)
Index Attribute [_ | Rows] Spreadsheet
-> (get-row Index Attribute Rows Spreadsheet)
Index _ _ _ -> (error "Index ~A not found" Index))
(define get-cell
Attribute [[Attribute Value] | _] Spreadsheet
-> (if (fixed-value? Value) Value (Value Spreadsheet))
Attribute [_ | Cells] Spreadsheet
-> (get-cell Attribute Cells Spreadsheet)
Attribute _ _ -> (error "Attribute ~A not found" Attribute))