You can solve this kind of problem in an elegant and type-safe way with type classes:
// Evidence that we can turn an A into some XML:
trait Markup[-A] { def toHtml(a: A): xml.Node }
def baseMarkup[A] = new Markup[A] { def toHtml(a: A) = xml.Text(a.toString) }
implicit def markup[A](implicit m: Markup[A] = baseMarkup[A]) =
new Markup[Iterable[A]] {
def toHtml(c: Iterable[A]) = <ul>
{ c.map(child => <li>{ m.toHtml(child) }</li>) }
</ul>
}
implicit def fromMarkup[A](a: A)(implicit m: Markup[A]) = new {
def toHtml = m toHtml a
}
This works for nested lists of arbitrary depth:
val messy = List(List(List("a")), List(List("b", "c"), List("c")))
val printer = new xml.PrettyPrinter(80, 2)
And then:
scala> printer format messy.toHtml
res1: String =
<ul>
<li>
<ul>
<li>
<ul>
<li>a</li>
</ul>
</li>
</ul>
</li>
<li>
<ul>
<li>
<ul>
<li>b</li>
<li>c</li>
</ul>
</li>
<li>
<ul>
<li>c</li>
</ul>
</li>
</ul>
</li>
</ul>
See my answer here for a more detailed explanation of the methods above.