我不認爲有這樣的內置任何東西。幸運的是,在Hamlet中使用輔助函數很容易。例如,如果您的項目是純字符串,則可以使用Data.List.intercalate
在它們之間添加逗號。
The values in the list are
#{intercalate ", " list}
and that is it.
如果你想做更有趣的事情,你可以編寫函數來處理哈姆雷特值。例如,下面是一個函數,它在列表中的哈姆雷特值之間添加逗號和「和」。
commaify [x] = x
commaify [x, y] = [hamlet|^{x} and ^{y}|]
commaify (x:xs) = [hamlet|^{x}, ^{commaify xs}|]
這使用^{...}
語法將一個Hamlet值插入另一個。現在,我們可以用它來寫一個逗號分隔的加下劃線的單詞列表。
The values in the list are
^{commaify (map underline list)}
and that is it.
這裏,underline
只是一個小的輔助函數產生的東西比純文本更有趣。
underline word = [hamlet|<u>#{word}|]
當呈現時,這給出了以下結果。
The values in the list are <u>foo</u>, <u>bar</u> and <u>baz</u> and that is it.
謝謝,我想這是很好的解決方案集合,雖然沒有像Haskell提供的那樣令人滿意。也許我應該爲一個特殊的'''loop'''變量提交一個補丁給hamlet。 –