Samstag, 3. Oktober 2009

trying Scala ... 3

List:

scala> var lst = List(1, 7, 2, 8, 5, 6, 3, 9, 14, 12, 4, 10)
res29: List[Int] = List(1, 7, 2, 8, 5, 6, 3, 9, 14, 12, 4, 10)


scala> def odd(inLst:List[Int]):List[Int]={
| if(inLst==Nil) Nil
| else if(inLst.head%2==1) inLst.head::odd(inLst.tail)
| else odd(inLst.tail)
| }
odd: (List[Int])List[Int]


def define a function/method
od() name
inLst:List[Int] => inLst parameter name with a List type of Integers
:List[Int] return a List of Integers
Nil
similar to none, None, null, void
inLst.head first element
inLst.tail all other elements after first element



scala> odd(lst)
res32: List[Int] = List(1, 7, 5, 3, 9)

Keine Kommentare:

Kommentar veröffentlichen