Previous Up Next

Module Layout

moduletypeLAYOUT=
sig
typeaseq
valaNil:aseq
valaStr:stringaseq
valaAppend:aseqaseqaseq
valaNewline:aseq
valaSpaces:intaseq
valaIndent:aseqaseq
valaDisplay:aseqstring
valaNum:intaseq
valaFWNum:intintaseq
valaConcat:aseqlistaseq
valaInterleave:aseqaseqlistaseq
valaInterleave_with_par:aseqaseqaseqaseqlistaseq
valaLayn:aseqlistaseq
end

moduleLayout=
(
struct
typeaseqRep=
ANil
AStrofstring
AAppendofaseqRepנaseqRep
AIndentofaseqRep
ANewline

letrecspacesi=if(i≤0)then""else""^spaces(i−1)

letrecflatten(col:int)(l:(aseqRepנint)list)=
match(col,l)with
(c,[])→""
∣(c,(ANil,indent)::tail)→flattenctail
∣(c,(AStrs,indent)::tail)→s^(flatten(c+(String.lengths))tail)
∣(c,(AAppend(seq1,seq2),indent)::tail)→
flattenc((seq1,indent)::(seq2,indent)::tail)
∣(c,(AIndentseq,indent)::tail)→
flattenc((seq,col)::tail)
∣(c,(ANewline,indent)::tail)→
"\n"^(spacesindent)^(flattenindenttail)

————– public part ———————-
typeaseq=aseqRep
letaNil=ANil
letaAppends1s2=AAppend(s1,s2)
letaStrs=AStrs
letaNewline=ANewline
letaSpacesi=AStr(spacesi)
letaIndents=AIndents
letaDisplays=flatten0[(s,0)]
letaNumi=AStr(string_of_inti)
letrecaConcatslist=
matchslistwith
[]→aNil
s::slaAppends(aConcatsl)

letrecaInterleavessl=
match(s,sl)with
(s,[])→aNil
∣(s,[s2])→s2
∣(s,s2::tail)→aAppends2(aAppends(aInterleavestail))

letrecaInterleave_with_parsp_openp_closesl=
match(s,sl)with
(s,[])→aNil
_→(aAppendp_open(aAppend(aInterleavessl)p_close))

letaFWNumwidthn=
letdigits=string_of_intnin
aAppend(aSpaces(width−(String.lengthdigits)))(aStrdigits)

letaLaynl=
letrec
lay_item(n,seq)=
aConcat[aFWNum4n;aStr")";aIndentseq;aNewline]
and
number_seqsl2m=match(l2,m)with
([],m)→[]
∣(s::tail,m)→(m,s)::number_seqstail(m+1)
in
aConcat(List.maplay_item(number_seqsl1))

end:LAYOUT)


Previous Up Next