object Main { val xs = 1 :: 2 :: 3 :: 4 :: 5 :: 6 :: 7 :: 8 :: Nil def nTy[A](xs: List[A], n: Int):A = { val h::t = xs (xs, n) match { case(Nil, _) => throw new Exception("Error") case(_, 1) => h case(_, _) => nTy(t, n - 1) } } nTy(xs, 3) def podzielPoN[A](a: List[A], n: Int): List[List[A]] = { def podzielPoN_tr[A](a: List[A], n: Int, b: List[A]): List[List[A]] = { if(n==0) List(b, a) else podzielPoN_tr(a.tail, n - 1, b ++ List(a.head)) } podzielPoN_tr(a,n, List[A]()) } podzielPoN(xs, 2) def podzielWgMod[A](list: List[Int], n: Int): List[List[Int]] = { def podzielWgMod_tr(xs: List[Int], a: List[Int], b: List[Int]): List[List[Int]] = { if (xs == Nil) List(a, b) else if (xs.head % n == 0) podzielWgMod_tr(xs.tail, a ++ List(xs.head), b) else podzielWgMod_tr(xs.tail, a, b ++ List(xs.head)) } podzielWgMod_tr(list, List(), List()) } podzielWgMod(xs, 2) def coNTy[A](xs: List[A], n: Int): List[A] = { def coNTy_it[A](x: List[A], i: Int): List[A] = { if (x == Nil) Nil else if (i == 1) x.head :: coNTy_it(x.tail, n) else coNTy_it(x.tail, i - 1) } coNTy_it(xs, n) } coNTy(xs, 3) }