Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Else return function

I'm taking the Coursera FP Principles in Scala course, and I'm having trouble with the last function in the week 2 assignment. I don't want the answer, but rather some help in understanding Scala functional composition. I think I have the right idea as to how to solve the problem, I'm just getting tripped up on the Scala-specific part.

The gist is: We've been given a type alias, defined as such:

type Set = Int => Boolean

Which I've interpreted to mean a Set is a function that takes an Int and returns a Bool.

We've also been tasked with finishing the function singletonSet, which takes an int and returns a Set. I've written it as such

def singletonSet(x: Int): Set = Set(x)
val three = singletonSet(3)
three(3) //True
three(5) //False

The function I'm having trouble with is the Map function, which has this signature:

def map(s: Set, p: Int => Int): Set

Which I've interpreted to mean A function that takes a set, transforms its elements with function P, and returns a new Set.

The pesudocode: Map returns a function that takes an int, and if that int exists in Set s, return a new Set with the transformed int X (or p(x)

The actual code that's breaking:

def map(s: Set, p: Int => Int): Set = {
  x => 
    if (s(x)) singletonSet(p(x))
    else p(x) => false
}

The error that I'm getting with this format is: 
    error: not a legal formal parameter.
    Note: Tuples cannot be directly destructured in method or function parameters.
    Either create a single parameter accepting the Tuple1,
    or consider a pattern matching anonymous function: `{ case (param1, param1) => ... }
        else p(x) => false 

I'm not grokking what's wrong with my implementation. A friend wrote my logic out in Haskell and found it to be successful, so I think my algorithm is correct (although I could be wrong). I'm struggling with the Scala implementation details. Any advice or guidance is much appreciated.

like image 434
thisisnotabus Avatar asked Aug 25 '16 20:08

thisisnotabus


People also ask

Is there a return statement in Scala?

The Scala programming language, much like Java, has the return keyword, but its use is highly discouraged as it can easily change the meaning of a program and make code hard to reason about.

Do you need return statements in Scala?

In scala, the return isn't required to be written with the resultant value. It evaluates that itself. You must've tried writing return explicitly to see if it gives you any error or something.

How do I return multiple values from a function in Scala?

You can return multiple values by using tuple. Function does not return multiple values but you can do this with the help of tuple.

What is the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .


1 Answers

Remember that you're dealing with a Set here, and Set is defined as a function that converts an Int to a Boolean. Therefore, your function needs to return the same thing:

def map(s: Set, p: Int => Int): Set = { 
  x => 
    if (s(x)) singletonSet(p(x))  // Returns a Set
    else p(x) => false            // Returns a Boolean
}

We can see that despite the input, you have two different output cases, which we know must be wrong. Now, lets also recall that you have other functions, and your definition of set and 'expand' what you're building:

def map(s: Set, p: Int => Int): (Int) => (Boolean)  //Expand 'Set' to int->boolean
  = (x: Int) => (Something that returns a boolean)

Your job is to figure out what that 'something' is, based on the semantics of map. I highly recommend looking around at other functions that return a boolean and ask how they might apply here. Specifically, you're looking for a function that will, for any integer provided, give you a transformation if that integer exists within the original set.

like image 164
Nathaniel Ford Avatar answered Oct 11 '22 14:10

Nathaniel Ford