Facebook
From Idiotic Cat, 6 Years ago, written in Scala.
Embed
Download Paste or View Raw
Hits: 375
  1. object Main {
  2.   val xs = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: Nil
  3.  
  4.   def nTy[A](xs: List[A], n: Int):A = {
  5.     val h::t = xs
  6.     (xs, n) match {
  7.       case(Nil, _) => throw new Exception("Error")
  8.       case(_, 1) => h
  9.       case(_, _) => nTy(t, n - 1)
  10.     }
  11.   }
  12.   nTy(xs, 3)
  13.  
  14.   def podzielPoN[A](a: List[A], n: Int): List[List[A]] = {
  15.    
  16.     def podzielPoN_tr[A](a: List[A], n: Int, b: List[A]): List[List[A]] = {
  17.       if(n==0) List(b, a)
  18.       else podzielPoN_tr(a.tail, n - 1, b ++ List(a.head))
  19.     }
  20.    
  21.     podzielPoN_tr(a,n, List[A]())
  22.   }
  23.   podzielPoN(xs, 2)
  24.  
  25.   def podzielWgMod[A](list: List[Int], n: Int): List[List[Int]] = {
  26.    
  27.     def podzielWgMod_tr(xs: List[Int], a: List[Int], b: List[Int]): List[List[Int]] = {
  28.       if (xs == Nil) List(a, b)
  29.       else if (xs.head % n == 0) podzielWgMod_tr(xs.tail, a ++ List(xs.head), b)
  30.       else podzielWgMod_tr(xs.tail, a, b ++ List(xs.head))
  31.     }
  32.     podzielWgMod_tr(list, List(), List())
  33.   }
  34.   podzielWgMod(xs, 2)
  35.  
  36.   def coNTy[A](xs: List[A], n: Int): List[A] = {
  37.    
  38.     def coNTy_it[A](x: List[A], i: Int): List[A] = {
  39.       if (x == Nil) Nil
  40.       else if (i == 1) x.head :: coNTy_it(x.tail, n)
  41.       else coNTy_it(x.tail, i - 1)
  42.     }
  43.     coNTy_it(xs, n)
  44.   }
  45.   coNTy(xs, 3)
  46. }

Replies to Untitled rss

Title Name Language When
Re: Untitled Wet Butterfly scala 6 Years ago.