About
higher order
mapo _ []=[] mapo f (x:xs)= f x : mapo f xs
foldR
map' :: (a -> b) -> [a] -> [b] map' f xs = foldr (\x acc -> f x : acc) [] xs
foldL
sum' :: (Num a) => [a] -> a sum' xs = foldl (\acc x -> acc + x) 0 xs
scala
object parray
{
def main(args:Array[String])
{
val greetStrings = new ArrayString
greetStrings(0) = "Hello"
greetStrings(1) = ", "
greetStrings(2) = "world!\n"
for (i <- 0 to 2)
print(greetStrings(i))
}
}
scala array
import Array._
object arraydemo
{
val myarr1:Array[Int] = new ArrayInt; //Creation method 1
val myarr2 = new ArrayString; //Creation method 2
val myarr3 = Array(1.5,2.3,6.4); //Creation method 3
val myarr4 = Array(1,2,3,4);
def main(args:Array[String])
{
myarr1(0)=20; //Assignments
myarr1(1)=50;
myarr1(2)=10;
myarr1(3)=30;
for(i<-myarr1) //Access Method 1
{
println(i);
}
for(i<- 0 to (myarr1.length-1)) //Access Method 2
{
println(myarr1(i));
}
for(i<-myarr2) //Prints default value if unassigned
{
println(i);
}
println(myarr3.length); //no of elements
val join = concat(myarr1,myarr4); //concatenates same array type
for(i<-join)
{
println(i);
}
}
}
scala arraydemo
scala list
object listdemo
{
val mylist1:List[Int] = List(1,2,3,4);
val mylist2:List[String]=List("John", "Tim", "Ken");
def main(args:Array[String])
{
println(mylist1);
println(mylist2);
//mylist1(0)=50; //List is immutable - cant change //println(mylist1);
println(50::mylist1); //cons :: to prepend println(mylist1); //value does'nt change by ::
println(1::3::5::Nil); //creates and prints a list
println(mylist2.head); //to print first item println(mylist2.tail); //prints last item
println(mylist2.isEmpty); //checks if list is empty
println(mylist1.reverse); //prints list in reverse
println(List.fill(5)(2)); //creates a list of 5 2's
println(mylist1.max); //prints max item in list
mylist2.foreach(println); //iterates thru each elt
var sum:Int=0; //sum of list using foreach mylist1.foreach(sum += _); println(sum);
for(i<-mylist2) //iteration using for { println(i); } println(mylist2(1)); //list indexing }
}
scala tuple
object tupdemo {
val mytuple1 = (1,2,"hi",false);
val mytuple2 = new Tuple4 (3,4,"hello",true); //no of items to be specified - allowed upto 22 elements
val mytuple3 = new Tuple3 (5,"welcome",(1,2)); //Tuple in tuple
def main(args:Array[String]) { println(mytuple1); println(mytuple2._3); //Tuples indexed from 1 println(mytuple3._3); //To print nested tuple println(mytuple3._3._2); //To print nested tuple element
mytuple1.productIterator.foreach //iterating thru a tuple { i => println(i); }
}
} scala tupdemo
object scala1{ def main(args:Array[String])={
print("is palindrome? ");println(palindrome("abcba"));
print("factorial of 30 = ");println(factorial(30));
print("count even = ");println(countEven(Array(1, 2, 3, 4, 5, 6)));
print("without odds = ");println(removeOdds(List(1, 2, 3, 4, 5, 6, 7, 8, 9)));
print("without odds = ");println(binarySearch(Array(1, 2, 3, 4, 5),3));
}
def palindrome(s:String): Boolean = { val sr:String = s.reverse; if (s == sr) {true} else {false} } def factorial(x: BigInt): BigInt = if (x == 0) 1 else x * factorial(x-1);
def countEven(x:Array[Int]): Int = { var count = 0; x.foreach(value => { if(value % 2 == 0) count += value; }) count }
def removeOdds(nums: List[Int]): List[Int] = { nums.filter(_ % 2 == 0) }
def binarySearch(arr: Array[Int], x: Int): Int = { var left = 0 var right = arr.length - 1 while (left <= right) { val mid = left + (right - left) / 2 if (arr(mid) == x) return mid else if (arr(mid) < x) left = mid + 1 else right = mid - 1 } -1 }
}