title: Returning a list of elements occurring in both lists - SML
body: <p>)I am just starting with SML and have been trying to write a function that takes two lists, L1 and L2, and returns a list of elements that occur in both. This is what I have so far:</p>
  
  <pre><code>fun exists x nil = false | exists x (h::t) = (x = h) orelse (exists x t);
  
      fun listAnd L1 nil = nil
       | listAnd nil L2 = nil
       | listAnd L1 L2 = if exists(hd(L1) L2) = true then hd(L1)::(listAnd(tl(L1) L2)) else listAnd(tl(L1) L2);
  </code></pre>
  
  <p>I am not really sure where is the error.</p>