<HTML>
<HEAD><TITLE>MSP examples: generating tables in various styles</TITLE></HEAD>
<BODY BGCOLOR="#fbf2e7">

<H1>MSP examples: generating tables in various styles</H1>

This page was generated <?MSP= Date.toString
(Date.fromTimeLocal(Time.now())) ?> to illustrate Mosml Server Pages.

<?MSP (* SML script, imperative style *)

fun impmktable oprname opr n m = 
    let fun mkcell r s = (print "<TD>"; print (Int.toString (opr(r,s))))
	fun mkhead i   = (print "<TH>"; print (Int.toString i))
	fun mkrow m r  = (print "<TR>"; mkhead r; 
			  List.tabulate(m, mkcell r); print "\n")
    in
	print "<TABLE BORDER><TR><TH>"; print oprname; 
	List.tabulate(m, mkhead); print "\n";
	List.tabulate(n, mkrow m);
	print "</TABLE>"
    end
?>

<?MSP (* SML script, functional style *)

fun funmktable oprname opr n m = 
    let open Msp 
	infix &&
	fun mkcell r s = $ "<TD>" && $ (Int.toString (opr(r,s)))
	fun mkhead i   = $ "<TH>" && $ (Int.toString i)
	fun tabulate(n, f) = List.foldr (op&&) Empty (List.tabulate(n, f)) 
	fun mkrow m r = $ "<TR>" && mkhead r && tabulate(m, mkcell r) && Nl
    in 
	$ "<TABLE BORDER><TR><TH>" && $ oprname && tabulate(m, mkhead) && Nl
	&& tabulate(n, mkrow m)
	&& $"</TABLE>"
    end
?>

<?MSP (* SML script, functional style, using the Msp HTML functions *)

fun mspmktable oprname opr n m = 
    let open Msp 
	infix &&
	fun mkcell r s = td($ (Int.toString (opr(r,s))))
	fun mkhead i   = th($ (Int.toString i))
	fun tabulate(n, f) = List.foldr (op&&) Empty (List.tabulate(n, f)) 
	fun mkrow m r = tr(mkhead r && tabulate(m, mkcell r)) && Nl
    in 
	tablea "BORDER" (tr (th ($oprname && tabulate(m, mkhead))) && Nl
		 	 && tabulate(n, mkrow m)	)
    end
?>

<?MSP 
   val n = Msp.%%#("nvalue", 6);
   val m = Msp.%%#("mvalue", 8);
?>

<H2>A multiplication table (generated by imperative code)</H2>

<?MSP val _ = impmktable "*" (op* ) n m ?>

<H2>A multiplication table (generated by functional code)</H2>

<?MSP$ funmktable "*" (op* ) n m ?>

<H2>An addition table (generated by functional code) </H2>

<?MSP$ funmktable "+" (op+ ) n m ?>

<H2>A subtraction table (generated by functional code using Msp HTML functions)</H2>

<?MSP$ mspmktable "-" (op- ) n m ?>

<P>

<?MSP val now = Date.fromTimeLocal(Time.now()) 

local
    open Substring
    fun drop0 s = string (dropl (fn c => c = #"0") (all s))
in 
    val shortmon = drop0 (Date.fmt "%m" now)
    val shortday = drop0 (Date.fmt "%d" now)
end
?>

<TABLE BORDER>
<TR><TD>Year              <TD><?MSP= Date.fmt "%Y" now ?>
<TR><TD>Month             <TD><?MSP= Date.fmt "%m" now ?>
<TR><TD>Danish date format<TD><?MSP= 
         shortday ^ "/" ^ shortmon ^ " " ^ Date.fmt "%Y" now ?>
<TR><TD>US date format    <TD><?MSP= 
	 shortmon ^ "/" ^ shortday ^ "/" ^ Date.fmt "%y" now ?>
<TR><TD>ISO date format   <TD><?MSP= Date.fmt "%Y-%m-%d" now ?>
<TR><TD>The time now is   <TD><?MSP= Date.fmt "%H:%M" now ?>
</TABLE>
</BODY>
</HTML>
