Yuki::RSSをHaskellに移植→中断

2005年1月13日 結城浩

適当な大きさの例題として、 Yuki::RSSをHaskellに移植しようと考えた。 作りかけたけれど、それ以前にハッシュテーブルや、 タグライブラリなどを作る/使うほうがよいとわかって中断。

\begin{code}

attr :: String -> String -> String
attr a v = a ++ "=\"" ++ v ++ "\""

xmlns_rdf   = attr "xmlns:rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns       = attr "xmlns" "http://purl.org/rss/1.0/"
xmlns_dc    = attr "xmlns:dc" "http://purl.org/dc/elements/1.1/"

xmlhead enc = "<?xml " ++ (attr "version" "1.0") ++ " " ++ (attr "encoding" enc) ++ " ?>"

data Rss = Rss Head Channel Body
data Head = Head [(String, String)]
data Channel = Channel [(String, String)]
data Body = Body [Item]
data Item = Item [(String, String)]

addItem :: Rss -> Item -> Rss
addItem (Rss h c (Body is)) i = (Rss h c (Body (is++[i])))

instance Show Rss where
    show (Rss h c b) = (headstr h) ++ (channelstr c) ++ (bodystr b)
        where
            itemstr :: Item -> String
            headstr :: Head -> String
            channelstr :: Channel -> String
            bodystr :: Body -> String

            headstr (Head []) = error "headstr"
            headstr (Head (("encoding",enc):xs)) = (xmlhead enc) ++ "...\n"
            headstr (Head ((x,y):xs)) = headstr (Head xs)
            headstr _ = error "headstr"

            channelstr _ = "...Channel...\n"

            bodystr (Body (x:xs)) = (itemstr x) ++ (bodystr (Body xs))
            bodystr _ = "\n"

            itemstr _ = "...item...\n"

rss1 = Rss
    (Head [
        ("version", "1.0"),
        ("encoding", "Shift_JIS")
    ])
    (Channel [
        ("title", "Site Title"),
        ("link", "http://www.example.com/index.html"),
        ("about", "http://www.example.com/rss.rdf"),
        ("description ", "The description of your site")
    ])
    (Body [
    ])

rss2 = addItem rss1
    (Item [
        ("title", "Item Title"),
        ("link", "http://www.example.com/item.html"),
        ("description", "Yoo, hoo, hoo"),
        ("dc_date", "2003-12-06T01:23:45+09:00")
    ])

\end{code}
Main> rss1
<?xml version="1.0" encoding="Shift_JIS" ?>...
...Channel...


Main> rss2
<?xml version="1.0" encoding="Shift_JIS" ?>...
...Channel...
...item...

2005年1月13日 11:10

記事一覧

2006 [ 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 ]
2005 [ 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 ]
2004 [ 01 | 02 | 03 | 04 | 05 | 06 | 07 | 08 | 09 | 10 | 11 | 12 ]

Bloglines

book(3) / char(2) / compiler(3) / craft(5) / data(7) / enum(1) / geb(2) / hawiki(1) / hugs(1) / info(1) / io(3) / list(2) / map(3) / monad(16) / nobsun(12) / report(4) / sicp(2) / soe(8) / suchthat(2) / yaht(8)

記事検索 サイト検索はこちら

豊かな人生のための四つの法則