Assignment #2 - funset - map function

492 views
Skip to first unread message

Leonard Siu

unread,
Oct 3, 2012, 11:05:34 AM10/3/12
to fp...@googlegroups.com
Is it just me or is an unbounded version of the map function "map(s: Set, f: Int => Int): Set" not possible?!

kwong ho

unread,
Oct 3, 2012, 12:20:10 PM10/3/12
to fp...@googlegroups.com
https://class.coursera.org/progfun-2012-001/forum/thread?thread_id=719

Andy's comment is really useful


map(S,f):

map(S,f)={b|cS,f(c)=b}

So, for a to be in map(S,f), then a must be one of the b's from the above expression and... the rest is an exercise for the reader :-)

the set of b's such that there exists a c in S, such that f(c)=b. In other words, it′s the set of numbers which are the result of applying the function f to some number in S.


What he say is :

You need to return a mapped result set (  ( Int ) => Boolean ).

In other words, the Boolean condition means you need to "find a mapped value f(c)  equal to the testing value (b) exists in S"

The results set satisfy that boolean condition is the mapped  result set.

This also a one liner of map function.


Example : input set S { 1, 3, 6, 7}  , map f ( (x: Int) => (x + 1) ) , 

if there exists a value f( value) ==  result int in S  { 1, 3, 6, 7} 

- if there has a value f( 1 ) == result int in S{1,3,6,7}

- f( value ) ---> (1+1) == result int in S{1, 3, 6, 7}  = 2

- f( value ) ---> (3+1) == result int in S{1, 3, 6, 7}  = 4

- f( value ) ---> (6+1) == result int in S{1, 3, 6, 7}  = 7

- f( value ) ---> (7+1) == result int in S{1, 3, 6, 7}  = 8

Xianming Mu

unread,
Oct 3, 2012, 1:22:39 PM10/3/12
to fp...@googlegroups.com
Hi leonard,

I didn't quite understand the meaning of "unbounded verison".

Anyway, since the Set is represented by the characteristic function, e.g. (x: Int) => x < 0 represents all the negtive integers,  what a map fucntion really does is that it uses the f to transform the condition. So whether or not some element e belongs to the new Set depends on if there's an old Set element equals to e after transformed by f.

In my implementation, I used exists as a helper function.

On Wed, Oct 3, 2012 at 11:05 PM, Leonard Siu <leona...@gmail.com> wrote:
Is it just me or is an unbounded version of the map function "map(s: Set, f: Int => Int): Set" not possible?!

--
You received this message because you are subscribed to the Google Groups "fp-cn" group.
To post to this group, send email to fp...@googlegroups.com.
To unsubscribe from this group, send email to fp-cn+un...@googlegroups.com.
Visit this group at http://groups.google.com/group/fp-cn?hl=en.
 
 



--
Regards,

Samuel

Leonard Siu

unread,
Oct 3, 2012, 1:31:26 PM10/3/12
to fp...@googlegroups.com
Thanks for all the enthusiastic response. I did not know there was a official forum until kwong ho post the link. I signed up late and was 3 days late for my first assignment :). Anyways, the information in the forum was really useful. I am so use to imperative programming which I find this new functional model is hurting my brain. I guess it is good exercise!
Reply all
Reply to author
Forward
0 new messages