Published
Edited
Jan 10, 2020
Insert cell
Insert cell
MULTIPLY_MAGNITUDES = qtyFn1 => qtyFn2 =>
qtyFn1(lib.ADD_MAGNITUDES(qtyFn2))
(lib.ZERO_MAGNITUDE)
Insert cell
Insert cell
MULTIPLY = num1 => num2 =>
lib.NUMBER(lib.NOT(lib.XOR(lib.SIGN(num1))(lib.SIGN(num2))))
(MULTIPLY_MAGNITUDES(lib.ABS(num1))(lib.ABS(num2)))
Insert cell
Insert cell
{
return {
"EIGHT_times_FIVE" : lib.to_integer(MULTIPLY(lib.PLUS_EIGHT)(lib.PLUS_FIVE))
, "MINUS_EIGHT_times_FIVE" : lib.to_integer(MULTIPLY(lib.MINUS_EIGHT)(lib.PLUS_FIVE))
, "EIGHT_times_MINUS_FIVE" : lib.to_integer(MULTIPLY(lib.PLUS_EIGHT)(lib.MINUS_FIVE))
, "MINUS_EIGHT_times_MINUS_FIVE" : lib.to_integer(MULTIPLY(lib.MINUS_EIGHT)(lib.MINUS_FIVE))
}
}
Insert cell
Insert cell
do_div =
div_next =>
count =>
remainder =>
divisor =>
// Should we continue with recursion?
lib.IS_LTE(divisor)(remainder)
// Yup, call div_next with an incremented counter, a new remainder
// (of the divisor subtracted from the old remainder) and the divisor
(fn =>
(div_next(lib.SUCC(count))
(lib.SUBTRACT(remainder)(divisor))
(divisor))
(fn))
// Nope, we've reached the end so return a PAIR formed from the
// the counter (now the quotient) and whatever is left over in the
// remainder
(lib.PAIR(count)(remainder))
Insert cell
Insert cell
_DIV = remainder => divisor => lib.Y(do_div)(lib.ZERO_MAGNITUDE)(remainder)(divisor)
Insert cell
Insert cell
IS_MAGNITUDE_LTE =
lte_qtyFn1 =>
lte_qtyFn2 =>
lib.HAS_ZERO_MAGNITUDE(lib.SUBTRACT_MAGNITUDES(lte_qtyFn1)(lte_qtyFn2))
Insert cell
Insert cell
do_div_magnitudes =
div_next =>
count =>
remainder =>
divisor =>
// Should we continue with recursion?
IS_MAGNITUDE_LTE(divisor)(remainder)
// Yup, call div_next with an incremented counter, a new remainder
// (of the divisor subtracted from the old remainder) and the divisor
(fn =>
(div_next(lib.SUCC_MAGNITUDE(count))
(lib.SUBTRACT_MAGNITUDES(remainder)(divisor))
(divisor))
(fn))
// Nope, we've reached the end so return a PAIR formed from the
// the counter (now the quotient) and whatever is left over in the
// remainder
(lib.PAIR(count)(remainder))
Insert cell
lib.to_boolean(IS_MAGNITUDE_LTE(lib.PLUS_EIGHT)(lib.PLUS_FIVE))
Insert cell
Insert cell
DIV = dividend => divisor =>
lib.NUMBER(lib.NOT(lib.XOR(lib.SIGN(dividend))(lib.SIGN(divisor))))
(lib.Y(do_div_magnitudes)
(lib.ZERO_MAGNITUDE)
(lib.ABS(dividend))
(lib.ABS(divisor))
)
Insert cell
Insert cell
magnitude_quot = numFn => lib.magnitude(lib.HEAD(lib.TAIL(numFn)))
Insert cell
magnitude_rem = numFn => lib.magnitude(lib.TAIL(lib.TAIL(numFn)))
Insert cell
div_result = numFn =>
({ isPos : lib.sign(numFn),
quot : magnitude_quot(numFn),
rem : magnitude_rem(numFn)
})
Insert cell
Insert cell
{
return {
"EIGHT_divided_by_FIVE" : div_result(DIV(lib.PLUS_EIGHT)(lib.PLUS_FIVE))
, "MINUS_EIGHT_divided_by_FIVE" : div_result(DIV(lib.MINUS_EIGHT)(lib.PLUS_FIVE))
, "EIGHT_divided_by_MINUS_FIVE" : div_result(DIV(lib.PLUS_EIGHT)(lib.MINUS_FIVE))
, "MINUS_EIGHT_divided_by_MINUS_FIVE" : div_result(DIV(lib.MINUS_EIGHT)(lib.MINUS_FIVE))

, "FIVE_divided_by_EIGHT" : div_result(DIV(lib.PLUS_FIVE)(lib.PLUS_EIGHT))
, "MINUS_FIVE_divided_by_EIGHT" : div_result(DIV(lib.MINUS_FIVE)(lib.PLUS_EIGHT))
, "FIVE_divided_by_MINUS_EIGHT" : div_result(DIV(lib.PLUS_FIVE)(lib.MINUS_EIGHT))
, "MINUS_FIVE_divided_by_MINUS_EIGHT" : div_result(DIV(lib.MINUS_FIVE)(lib.MINUS_EIGHT))
}
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more