Common Lisp guys have their CL-WHO, which makes HTML templating integrated with the \"main\" language thus making the task easier. For those who don\'t know CL-WHO, it looks
Haskell has an HTML combinator library that is not all that different from CL-WHO. The lazy functional approach to programming, though, does result in a much different idiomatic iteration structure than the loop facilities in Common Lisp:
import Data.Char
import Data.List
import Text.Html
-- from http://fawcett.blogspot.com/2007/08/roman-numerals-in-haskell.html
import RomanNumerals
-- Simple roman numeral conversion; returns "" if it cannot convert.
rom :: Int -> String
rom r = let m = toRoman r
in (map toUpper . maybe "" id) m
-- Group a list N elements at a time.
-- groupN 2 [1,2,3,4,5] == [[1,2],[3,4],[5]]
groupN n [] = []
groupN n xs = let (a, b) = splitAt n xs in a : (groupN n b)
pink = "pink" -- for convenience below; green is already covered by Text.Html
rom_table = table ! [border 0, cellpadding 4] << trs
where
-- a list of entries
trs = map (rom_tr . map rom_td) rom_array
-- generates a from a list of s
rom_tr tds = tr ! [align "right"] << tds
-- generates a given a numeral and a color
rom_td (r, c) = td ! [bgcolor c] << r
-- our 5 x 5 array (list x list) of numerals and colors
rom_array = (groupN 5 . take 25) rom_colors
-- a theoretically infinite list of pairs of roman numerals and colors
-- (practically, though, the roman numeral library has limits!)
rom_colors = zip (map rom [1..]) colors
-- an infinite list of alternating green and pink colors
colors = cycle [green, pink]
main = let s = prettyHtml rom_table
in putStrLn s
I should note there's also a little combinator library in Text.Html for composing tables using "above" and "beside" operators to calculate row/column spanning, but it's a little too simplistic in terms of applying attributes to duplicate this example exactly, and we don't need the fancy splitting of rows and columns.
- 热议问题