Convert List[String] to List[Char]

1,136 views
Skip to first unread message

TavMem

unread,
May 18, 2012, 7:56:49 AM5/18/12
to scala...@googlegroups.com
This is from Listing 3-5 (p.66) in the "Begining Scala" book.
How do I convert a List[String] to a List[Char]  ?
(I've tried several alternatives, and none worked.)

tom@xps ~/t/c03 $ cat rn
def roman(in: List[Char]): Int = in match {
  case 'I' :: 'V' :: rest => 4 + roman(rest)
  case 'I' :: 'X' :: rest => 9 + roman(rest)
  case 'I' :: rest => 1 + roman(rest)
  case 'V' :: rest => 5 + roman(rest)
  case 'X' :: 'L' :: rest => 40 + roman(rest)
  case 'X' :: 'C' :: rest => 90 + roman(rest)
  case 'L' :: rest => 10 + roman(rest)
  case 'C' :: 'D' :: rest => 400 + roman(rest)
  case 'C' :: 'M' :: rest => 900 + roman(rest)
  case 'C' :: rest => 100 + roman(rest)
  case 'D' :: rest => 500 + roman(rest)
  case 'M' :: rest => 1000 + roman(rest)
  case _ => 0
}

val a = args
val b = a.toList
val c = roman(b)
println(c)

---------------------------------------------------------
tom@xps ~/t/c03 $ scala rn IV
/home/tom/t/c03/rn:19: error: type mismatch;
 found   : List[String]
 required: List[Char]
val c = roman(b)
              ^
one error found

Dave

unread,
May 18, 2012, 8:43:45 AM5/18/12
to scala-user
val c = roman(b(0).toList)
or
val c = roman(b.head.toList)

A String is already a List[Char]
so List[String] is List[List{Char]]
so you need to pass only one element of the List

Dave

unread,
May 18, 2012, 8:48:33 AM5/18/12
to scala-user
Of course you can do it in an early stage
val a = args(0)
> > one error found- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

bhericher

unread,
May 18, 2012, 10:18:57 AM5/18/12
to scala-user
scala> val l=List("abc","def")
l: List[java.lang.String] = List(abc, def)

We have a List of Strings

Then :

scala> l map {x => x.toList}
res1: List[List[Char]] = List(List(a, b, c), List(d, e, f))

We have a List of List of Chars

finally :

scala> (l map {x => x.toList}).flatten
res2: List[Char] = List(a, b, c, d, e, f)

We have a List of Char

Is it what you expected?

Benoit

Kevin Wright

unread,
May 18, 2012, 10:48:31 AM5/18/12
to bhericher, scala-user
For moar fun & profit:

 l flatMap {_.toList}
--
Kevin Wright
mail: kevin....@scalatechnology.com
gtalk / msn : kev.lee...@gmail.com
vibe / skype: kev.lee.wright
steam: kev_lee_wright

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra

Kevin Wright

unread,
May 18, 2012, 10:52:19 AM5/18/12
to bhericher, scala-user
Or, better still, String will be implicitly converted to a List[Char] when supplied as an argument to any method expecting this type.  So we can:

strings flatMap {x => roman(x)}

or

strings flatMap {roman(_)}

or even
(point free style)

strings flatMap {roman}

Derek Williams

unread,
May 18, 2012, 11:54:12 AM5/18/12
to bhericher, scala-user
On Fri, May 18, 2012 at 8:18 AM, bhericher <bher...@free.fr> wrote:
scala> val l=List("abc","def")
l: List[java.lang.String] = List(abc, def)

We have a List of Strings

Then :

scala> l map {x => x.toList}
res1: List[List[Char]] = List(List(a, b, c), List(d, e, f))

We have a List of List of Chars

finally :

scala> (l map {x => x.toList}).flatten
res2: List[Char] = List(a, b, c, d, e, f)

We have a List of Char

Or more simply: 

scala> List("hello", "world").flatten
res0: List[Char] = List(h, e, l, l, o, w, o, r, l, d)


--
Derek Williams

Reply all
Reply to author
Forward
0 new messages