;N int.cam - a CAMILA library for small integers (-32768..32767) ;A J.N. Oliveira (jno@di.uminho.pt) ;D ; This library provides a few functions useful in simple ; integer arithmetics. ; Note that CAMILA type INT ranges from -32768 to 32767. ; For long integer arithmetics other data types are required. ;E ; ; intAvg(l,p) averages sequence l of integers, p adds decimal precision. ; ( def _ops ( plus _ops ( makeff ( ( quote intAvg ) ( quote ( ( ( LIST INT ) ( INT ) ) ( INT ) ) ) ) ) ) ) ;--------------------- ( def intAvg lambda ( l p ) ( let ( ( a ( length l ) ) ( b ( mul ( intSUM l ) ( intPot 10 p ) ) ) ) ( div b a ) ) ) ;--------------------- ; ; intSUM(l) sums sequence l of integers. ; ( def _ops ( plus _ops ( makeff ( ( quote intSUM ) ( quote ( ( ( LIST INT ) ) ( INT ) ) ) ) ) ) ) ;--------------------- ( def intSUM lambda ( l ) ( reduce 0 add l ) ) ;--------------------- ; ; intMIN(s) computes minimum of a set of INT. ; ( def _ops ( plus _ops ( makeff ( ( quote intMIN ) ( quote ( ( ( SET INT ) ) ( INT ) ) ) ) ) ) ) ;--------------------- ( def intMIN lambda ( s ) ( reduce 32767 min s ) ) ;--------------------- ; ; intMIN(s) computes maximum of a set of INT. ; ( def _ops ( plus _ops ( makeff ( ( quote intMAX ) ( quote ( ( ( SET INT ) ) ( INT ) ) ) ) ) ) ) ;--------------------- ( def intMAX lambda ( s ) ( reduce 0 max s ) ) ;--------------------- ; ; intMUL(l) reduces sequence l of integers via multiplication. ; ( def _ops ( plus _ops ( makeff ( ( quote intMUL ) ( quote ( ( ( LIST INT ) ) ( INT ) ) ) ) ) ) ) ;--------------------- ( def intMUL lambda ( l ) ( reduce 1 mul l ) ) ;--------------------- ; ; intPot(a,b) computes a^b. ; ( def _ops ( plus _ops ( makeff ( ( quote intPot ) ( quote ( ( ( INT ) ( INT ) ) ( INT ) ) ) ) ) ) ) ;--------------------- ( def intPot lambda ( a b ) ( let ( ( l ( seq a ( from i ( inseg b ) ) ) ) ) ( intMUL l ) ) ) ;--------------------- ; ; intSet2IncIntSeq(s) sorts s in INT-increasing order. ; ( def _ops ( plus _ops ( makeff ( ( quote intSet2IncIntSeq ) ( quote ( ( ( SET INT ) ) ( LIST INT ) ) ) ) ) ) ) ;--------------------- ( def intSet2IncIntSeq lambda ( s ) ( if ( equal s ( makeset ) ) ( makeseq ) ( let ( ( m ( intMIN s ) ) ( t ( set x ( from x s ( nequal x m ) ) ) ) ) ( append ( makeseq m ) ( intSet2IncIntSeq t ) ) ) ) ) ;--------------------- ; ; intSet2IntSeq(s) sorts s in INT-decreasing order. ; ( def _ops ( plus _ops ( makeff ( ( quote intSet2IntSeq ) ( quote ( ( ( SET INT ) ) ( LIST INT ) ) ) ) ) ) ) ;--------------------- ( def intSet2IntSeq lambda ( s ) ( if ( equal s ( makeset ) ) ( makeseq ) ( let ( ( m ( intMAX s ) ) ( t ( set x ( from x s ( nequal x m ) ) ) ) ) ( append ( makeseq m ) ( intSet2IntSeq t ) ) ) ) ) ;--------------------- ; ; intFF2index(ff) builds index of ff (range INT-increasing ordering). ; ( def _ops ( plus _ops ( makeff ( ( quote intFF2index ) ( quote ( ( ( FF A INT ) ) ( LIST A ) ) ) ) ) ) ) ;--------------------- ( def intFF2index lambda ( ff ) ( if ( equal ff ( makeff ) ) ( makeseq ) ( let ( ( m ( intMIN ( ran ff ) ) ) ( X ( set a ( from a ( dom ff ) ( equal ( ap ff a ) m ) ) ) ) ( l ( seq a ( from a X ) ) ) ) ( append l ( intFF2index ( ds ff X ) ) ) ) ) ) ;--------------------- ; ; intAsRealRound(n,i) regards n as a real number with i decimal places ; and rounds it. ; ( def _ops ( plus _ops ( makeff ( ( quote intAsRealRound ) ( quote ( ( ( INT ) ( INT ) ) ( INT ) ) ) ) ) ) ) ;--------------------- ( def intAsRealRound lambda ( n i ) ( let ( ( p ( intPot 10 i ) ) ( d ( div n p ) ) ( r ( rem n p ) ) ) ( if ( lt r ( div p 2 ) ) d ( add d 1 ) ) ) ) ;--------------------- ; ; intAsReal2STR(n,i) regards n as a real number with i decimal places ; and converts into to a string. ; ( def _ops ( plus _ops ( makeff ( ( quote intAsReal2STR ) ( quote ( ( ( INT ) ( INT ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def intAsReal2STR lambda ( i d ) ( let ( ( x ( intPot 10 d ) ) ( d ( div i x ) ) ( r ( rem i x ) ) ) ( strcat ( itoa d ) "." ( itoa r ) ) ) ) ;---------------------