// loans.exc // Used and explained in // "How to Document a Spreadsheet: an // Exercise with Cash-Flow and Loans", // http://www.j-paine.org/dobbs/loans.html . macro table true : -> boolean. true[] = 1=1. constant no_of_loans = 4. type loans_range = 1:no_of_loans. table has_ceiling : loans_range -> boolean. table ceiling : loans_range -> currency. table initial_loan : loans_range -> currency. table total_loan_at_start_of_period : loans_range time_range -> currency. table total_loan_at_end_of_period : loans_range time_range -> currency. total_loan_at_start_of_period[ l, 1 ] = initial_loan[ l ]. total_loan_at_start_of_period[ l, t>1 ] = total_loan_at_end_of_period[ l, t-1 ]. total_loan_at_end_of_period[ l, t ] = total_loan_at_start_of_period[ l, t ] + lent_during_period[ l, t ]. lent_during_period[ l, t ] = if( isna( first_that_can_supply_wants[t] ) , 0 , if( l = first_that_can_supply_wants[ t ] , want_to_borrow_during_period[ t ] , 0 ) ). table lent_during_period : loans_range time_range -> currency. table can_supply_wants : loans_range time_range -> boolean. can_supply_wants[ l, t ] = or( not( has_ceiling[l] ) , want_to_borrow_during_period[t] + total_loan_at_start_of_period[l,t] <= ceiling[l] ). table first_that_can_supply_wants : time_range -> general. first_that_can_supply_wants[ t ] = match( true[], can_supply_wants[ all, t ], 0 ). actually_borrowed_during_period[ t ] = sum( lent_during_period[ all, t ] ). layout( 'Loans' , rows( heading , row( skip, has_ceiling as x ) , heading , row( skip, ceiling as x ) , heading , row( skip, initial_loan as x ) , row( skip(1,2) ) , heading , row( copy(time), total_loan_at_start_of_period as xy, skip, total_loan_at_end_of_period as xy ) , row( skip(1,2) ) , heading , row( copy(time), can_supply_wants as xy, skip, lent_during_period as xy ) ) ).