Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This code has a few problems:</p> <p>It seems that you are not realizing that <code>List[T]</code> is an <em>immutable</em> type, meaning you cannot change its value once created. So if you have a <code>List[T]</code> and you call the <code>::</code> method to prepend a value, the function returns a new list and leaves your existing one unchanged. Scala has mutable collections such as <code>ListBuffer</code> which may behave more like you expect. So when you <code>return l</code>, you're actually returning the original list.</p> <p>Also, you have the order wrong in using <code>::</code>. It should go <code>i :: l</code>, since <code>::</code> is a right-binding function (because it ends with a <code>:</code>).</p> <p>Lastly, in your union method you are doing <code>(other contains i)</code>. Maybe it's just the Scala syntax that's confusing you, but this is the same as doing <code>(other.contains(i))</code> and clearly <code>contains</code> is not a defined method of <code>parentSet</code>. It is a method on the <code>List[T]</code> type, but you're not calling <code>contains</code> on a list.</p> <p>You tagged this as homework so I'm not going to fix your code, but I think you should </p> <ol> <li><p>Look at some examples of correct Scala code involving lists, try <a href="http://anders.janmyr.com/2009/10/lists-in-scala.html" rel="nofollow">here</a> for starters</p></li> <li><p>Play around in the Scala REPL and try creating and working with some lists, so you get a feel for how immutable collections work.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload