( load "seq.met" ) ;--------------------- ;N str.cam - a CAMILA library for the finite string STR data type ;A J.N. Oliveira (jno@di.uminho.pt) ;D ; This library provides a few functions useful in simple ; string manipulation. ; Note that strings in CAMILA type STR are of limited length. ; For longer strings resort to txt.cam. ;E ; strMIN(s) computes least of a set of strings ( def _ops ( plus _ops ( makeff ( ( quote strMIN ) ( quote ( ( ( SET STR ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strMIN lambda ( s ) ( reduce ( chr 255 ) min s ) ) ;--------------------- ; ; strCAT(l) catenates all strings in sequence of strings l. ; ( def _ops ( plus _ops ( makeff ( ( quote strCAT ) ( quote ( ( ( LIST STR ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strCAT lambda ( l ) ( if ( equal l ( makeseq ) ) "" ( strcat ( hd l ) ( strCAT ( tl l ) ) ) ) ) ;--------------------- ; ; strCenter(s,l) centers s in a string of length l, ; the extra characters being " ". ; Pre-condition: l >= strlen(s) ; ( def _ops ( plus _ops ( makeff ( ( quote strCenter ) ( quote ( ( ( STR ) ( INT ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strCenter lambda ( s l ) ( if ( equal l ( strlen s ) ) s ( let ( ( x ( sub l ( strlen s ) ) ) ( m ( div x 2 ) ) ( r ( rem x 2 ) ) ( n ( add m r ) ) ) ( strcat ( strFill " " m ) s ( strFill " " n ) ) ) ) ) ;--------------------- ; ; Ibidem strCenter(s,l) left-justifying instead of centering. ; Pre-condition: l >= strlen(s) ; ( def _ops ( plus _ops ( makeff ( ( quote strLeft ) ( quote ( ( ( STR ) ( INT ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strLeft lambda ( s l ) ( if ( equal l ( strlen s ) ) s ( strcat ( strleft s ( sub l 1 ) ) " " ) ) ) ;--------------------- ; ; Ibidem strCenter(s,l) right-justifying instead of centering. ; Pre-condition: l >= strlen(s) ; ( def _ops ( plus _ops ( makeff ( ( quote strRight ) ( quote ( ( ( STR ) ( INT ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strRight lambda ( s l ) ( if ( equal l ( strlen s ) ) s ( strcat " " ( strright s ( sub l 1 ) ) ) ) ) ;--------------------- ; ; strFill(s,l) replicates s l times. ; Pre-condition: l >= 0 ; ( def _ops ( plus _ops ( makeff ( ( quote strFill ) ( quote ( ( ( STR ) ( INT ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strFill lambda ( s l ) ( if ( equal l 0 ) "" ( strcat s ( strFill s ( sub l 1 ) ) ) ) ) ;--------------------- ;RETURN do(x<-"", ; n<-l, ; while((n>0),do(x<-strcat(s,x),n<-n.-1)), ; x); ; ; strSeq2STR(l) converts sequence of strings l into a single string, ; separated by s (typically, s=","). ; ( def _ops ( plus _ops ( makeff ( ( quote strSeq2STR ) ( quote ( ( ( LIST STR ) ( STR ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strSeq2STR lambda ( l s ) ( strCAT ( seqAddSep l s ) ) ) ;--------------------- ; ; strSet2STR is the counterpart of strSeq2STR for finite sets. ; ( def _ops ( plus _ops ( makeff ( ( quote strSet2STR ) ( quote ( ( ( SET STR ) ( STR ) ) ( STR ) ) ) ) ) ) ) ;--------------------- ( def strSet2STR lambda ( X s ) ( strSeq2STR ( seq x ( from x X ) ) ) ) ;--------------------- ; ; strFF2index(ff) builds index of ff (range STR-increasing ordering). ; ( def _ops ( plus _ops ( makeff ( ( quote strFF2index ) ( quote ( ( ( FF A STR ) ) ( LIST A ) ) ) ) ) ) ) ;--------------------- ( def strFF2index lambda ( ff ) ( if ( equal ff ( makeff ) ) ( makeseq ) ( let ( ( m ( strMIN ( ran ff ) ) ) ( X ( set a ( from a ( dom ff ) ( equal ( ap ff a ) m ) ) ) ) ( l ( seq a ( from a X ) ) ) ) ( append l ( strFF2index ( ds ff X ) ) ) ) ) ) ;---------------------