Scala: searching array element using recursion

HI,

Below is an example of searching of elements in an array using scala monomorphic function as well recursion

object arraysearch extends App {

def searcharray(ss:Array[String],key:String):Int=
{
def loop(n:Int):Int=
{
if(ss(n)==key) n //
else if (n<=0) -1
else loop(n-1)
}
loop(ss.length-1)  // pass the arary(length -1) in loop function  recursion

}
println(searcharray(Array(“hii”,”how”,”are”,”you”),”are”))
}

 

Thanks please comment and follow

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s