;------- UI buttons ------------------------------------------------------------ _fn<-"jno.cam"; v() = vi(_fn); vv() = sh(strcat("vi ",_fn)); lo()= CONC(< < x, "\n" > | x <- ops >); FUNC error(m:STR):SYM RETURN let (x=princ(m)) in 'error; FUNC Set2STR(X:X-set,s:STR):STR RETURN if X=={} then "" else let (a = , b = < head(a) > ^ ) in Strcat(b); ;------- STR extra functionality ---------------------------------------------- FUNC STRMIN(s:STR-set):STR RETURN min-orio(chr(255),s); stdDump(l) = do(foreach(s,l,do(princ(s))),""); FUNC strcenter(s:STR,l:INT):STR ; pre: l >= strlen(s) RETURN if l==strlen(s) then s else let (x=l.-strlen(s),m=x./2,r=rem(x,2),n=m.+r) in strcat(strfill(" ",m),s,strfill(" ",n)); FUNC strleft(s:STR,l:INT):STR ; pre: l >= strlen(s) RETURN if l==strlen(s) then s else strcat(strleft(s,l.-1)," "); FUNC strright(s:STR,l:INT):STR ; pre: l >= strlen(s) RETURN if l==strlen(s) then s else strcat(" ",strright(s,l.-1)); FUNC strfill(s:STR,l:INT):STR ;if l==0 then "" else strcat(s,strfill(s,l.-1)); RETURN do(x<-"", n<-l, while((n>0),do(x<-strcat(s,x),n<-n.-1)), x); ;------- STR-list functionality ------------------------------------------------ ; map0s2 : Atom * Atom-list -> Atom-list map0s2(d,l) = < if a==0 then d else a | a <- l >; FUNC addStrSep(l:STR-list,s:STR):STR-list RETURN if l==<> then <> else < head(l) > ^ < strcat(s,x) | x <- tail(l) >; ; Strcat : STR-list --> STR Strcat(l) = if l==<> then "" else strcat(head(l),Strcat(tail(l))); FUNC strList2File(fn:STR,p:STR-list):SYM RETURN do(sh(strcat("rm ",fn)), _f<- fopen(fn,"w"), foreach(x,p,if x=="" then princ(".") else fPuts(x,_f)), fclose(_f), sh(strcat("ls -l ",fn)), ); ;------- Recursive STR-lists -------------------------------------------- ; RecSTRlist = ( STR + RecSTRlist )-list ; flatten: RecSTRlist -> STR-list flatten(l) = CONC(< if is-STR(x) || is-INT(x) || is-SYM(x) then < x > else flatten(x) | x <- l >); ;------- BOOL extra functionality --------------------------------------- forall(B)= subset(B,{true}); ;------- INT extra functionality ---------------------------------------------- FUNC MIN(s:INT-set):INT RETURN min-orio(32000,s); FUNC MAX(s:INT-set):INT RETURN max-orio(0,s); FUNC pot(a:INT,b:INT):INT RETURN let (l=< a | i <- inseg(b) >) in MUL(l); FUNC INTasRealRound(n:INT,i:INT):INT RETURN let (p=pot(10,i), d= n ./ p, r= rem(n,p)) in if (r < p./2) then d else d .+ 1; FUNC INTasReal2STR(i:INT,d:INT):STR RETURN let (x=pot(10,d), d=div(i,x), r=rem(i,x)) in strcat(itoa(d),".",itoa(r)); perc(a,b) = if (a<33) then INTasRealRound(a .* 1000 ./ b,1) ; else if (a<44) ; then INTasRealRound(a .* 375 .* 3 ./b .*2,1) else if (a<66) then INTasRealRound(a .* 500 ./b .*2,1) else a .* 100 ./ b; FUNC avg(l:STR-list,p:INT):INT RETURN let (a=length(l), b=SUM(l) .* pot(10,p)) in b ./ a; ;------- funcoes polimorficas primitivas ----------------------- FUNC AtomFF2STRFF(fk:Atom->Atom):STR->STR RETURN if fk==[] then [] else let(k=choice(dom(fk)), x = fk[k], kk= if is-STR(k) then k else itoa(k), xx= if is-STR(x) then x else itoa(x)) in [ kk -> xx ] + AtomFF2STRFF(fk \ {k}); FUNC ffinv(ff:A->B):B->A-set RETURN [ b -> { a | a<- dom(ff): ff[a]==b } | b <- ran(ff) ]; FUNC fflist(ff:A->B,l:A-list,d:B):B-list RETURN < if a in dom(ff) then ff[a] else d | a <- l >; PLUS(s) = if s=={} then [] else plus-orio([],s); SUM(l) = if l==<> then 0 else add-orio(0,l); MUL(l) = if l==<> then 1 else mul-orio(1,l); ;------- X-list extra functionality ----------------------------------------- invl(l) = if l==<> then <> else invl(tail(l))^; inseq(n) = if n==0 then <> else cons(n,inseq(n.-1)); blast(l) = tail(invl(l)); xlist2ff(a) = [ x -> nth(x,a) | x <- inseg(length(a)) ]; ;sublista de n elementos a partir do i-esimo subl(l,i,n) = if n==0 then <> else if l == <> then <> else let (h=hd(l), t=tl(l)) in if i==1 then ^subl(t,i,n.-1) else subl(t,i.-1,n); ;listagem ordenada decrescente de conjunto de INT INTset2INTlist(s) = if s=={} then <> else let (m=MAX(s), t={ x | x <- s: x != m}) in ^INTset2INTlist(t); ;indice de uma ff (crescente por contradominio de STRs) ;index : (A->STR) -> A-list index(ff)= if ff==[] then <> else let (m=STRMIN(ran(ff)), X={ a | a <- dom(ff): ff[a] == m }, l=< a | a <- X >) in l ^ index(ff\X); ;indice de uma ff (crescente por contradominio de INTs) ;indexOnINT : (A->INT) -> A-list indexOnINT(ff)= if ff==[] then <> else let (m=MIN(ran(ff)), X={ a | a <- dom(ff): ff[a] == m }, l=< a | a <- X >) in l ^ indexOnINT(ff\X); ;------- I/O fixup ------------------------------------------------------- FPuts(s,f)=progn(_s<-s,m2write(f,_s)); fPuts(s,f)=progn(_s<-s,m2write(f,_s)); fPuts(s,f)=fputs(s,f);