functional programming - Scala and Writing map functions -
so suppose have function expects set definition int => boolean
, function f
this:
def map(s: set, f: int => int): set = {}
now how apply f
each element of set s
.
def map(s: set, f: int => int): set = { (i: int) => f(s(i)) }
which ofcourse incorrect because in f(s(i))
, 's(i)' returns boolean, , can't apply f
on it. problem how access each element of set , apply f
on it?
this question part of coursera's functional programming scala course.
a goal of course understand functional model, , in case, how set can represented function (called charasteristic function set).
instead of final solution, here's hint on how reason problem:
given characteristic function f: int => boolean
defines set , x
, int, if f(x) == true
, x belongs set. now, if have function g:int=>int
want map on set, want apply function elements know belong set: if (f(x)) g(x)
.
try apply thinking exercise.
Comments
Post a Comment