Using Scala pattern matching to find whether a List contains a value -


i've seen range of different matches can against list using pattern matching. possible use pattern matching identify if (and ideally which) element in list contains value x? this:

def contains(text: list[char], chr: char): boolean = text match{   case *pattern matching chr value in text* => true   case _ => false } 

just trying learn can , can't do.

(i know can use text.contains(chr), btw)

thanks, peter

what have seen use matching on list recurse, in case probably:

def contains(text: list[char], chr: char): boolean = text match{   case head :: tail  => head.equals(chr) || contains(tail, chr)   case nil => false } 

i don't think can use match kind of operations because of how unapply method of list defined, theoretically if know length of list write like

case x1 :: x2 :: x3 // , on 

but guess can see not practical nor ideal param-matching.


Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -