Created
September 5, 2017 17:57
-
-
Save denismaster/d168119bbba859de5d50c2c3bafed41e to your computer and use it in GitHub Desktop.
ПСЯПр 1 лаба
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package FirstLab | |
object Application{ | |
def main(args: Array[String]):Unit={ | |
println("List:"); | |
val list = List(1,2,3,4,5,6,7,8,9,10); | |
printList(list); | |
println(); | |
println("Recursive func:") | |
printList(recursive(list)) | |
println(); | |
println("Tail recursive func:") | |
printList(tailRecursive(list)); | |
} | |
def recursive(list: List[Int]): List[Int]={ | |
if(list==Nil) return Nil; | |
return list.head :: list.head :: recursive(list.tail); | |
} | |
def tailRecursive(list: List[Int]): List[Int] ={ | |
def iter(x: List[Int], result:List[Int]):List[Int]={ | |
if(x==Nil) result; | |
else iter(x.tail, x.head::x.head::result); | |
} | |
iter(list, Nil).reverse; | |
} | |
def printList(list: List[Int]): Unit ={ | |
if(list==Nil) return; | |
print(list.head); | |
print(" "); | |
printList(list.tail); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment