Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8647595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:06:31+00:00 2026-06-12T13:06:31+00:00

I am taking a Coursera Functional Programming in Scala class. This is the second

  • 0

I am taking a Coursera Functional Programming in Scala class. This is the second week and I hit a wall. In the assignment we are working with Sets, but not the kind of Set we all meet in Java, for example. It is a Set that returns true if the value is in there and false otherwise. They say it’s not a container, it’s just a function.

To get it clear, I need your help. I don’t want you to solve my assignment, it’s just an example that I want to get the idea of what I should do.

/**
   * We represent a set by its characteristic function, i.e.
   * its `contains` predicate.
   */
  type Set = Int => Boolean

 /**
   * Indicates whether a set contains a given element.
   */
  def contains(s: Set, elem: Int): Boolean = s(elem)

 /**
   * Returns the set of the one given element.
   */
  def singletonSet(elem: Int): Set = Set(elem)

 /**
   * Returns the union of the two given sets,
   * the sets of all elements that are in either `s` or `t`.
   */
  def union(s: Set, t: Set): Set = ???  

This is the code. In the singletonSet I guess the way to solve it is to return the Set(elem), right?

If that is good, how am I supposed to make the union between the two? I am not new to programming but I can’t see any way to do it. Since I shouldn’t return a “set” of numbers.

This is what another student told me about sets: “But all a “Set” is is a function that takes an Int and returns a Boolean (Int => Boolean). Any function that takes an Int and returns a Boolean fits the type ‘Set’.”

What I tried in the union function is to have something like:

def union(s: Set, t: Set): Set = (s | t) //value | not a member of Int => Boolean  

Any help would be appreciated 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T13:06:32+00:00Added an answer on June 12, 2026 at 1:06 pm

    It seems the wall you are hitting is that you are unfamiliar with defining functions in Scala. In this particular case you need to define functions of type Int => Boolean, they take an Int and return a Boolean.

    Here are some examples of function literals of type Int => Boolean. Try them in the Scala console or the Scala IDE worksheet:

    (x: Int) => true
    (x: Int) => false
    (x: Int) => x == 2
    (x: Int) => x == 10
    (x: Int) => x == 2 || x == 10
    (x: Int) => x % 2 == 0
    

    Then all you have to do for the assignment is to use the same syntax, starting with (x: Int) => and then translate the meaning of union, intersect, … into the right hand side of the expression.

    Part of learning is giving it a genuine effort. I believe you can resubmit the solution multiple times, so don’t hesitate to submit and iterate if you don’t get 10/10 on the first try. All you need is compiling code. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am taking up algorithm course on coursera,and I am stuck on this particular
Taking this thread to the next level and now making a Main.as class to
Taking this question about beautiful XSL but getting more specific, how should I refactor
Taking Fabien Potencier's example : class User { function __construct($storage) { $this->storage = $storage;
Taking advice from this post , I purchased a copy of 'The C Programming
Taking this file as an example, I'm trying to read the data in a
Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
Taking a JavaScript object with 4 properties: function Object() { this.prop1; this.prop2; this.prop3; this.prop4;
Taking Sql this quarter and not having any luck with the following question: The
Taking beginning Data Structures in C# class, trying to make a searchable arraylist of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.