0

I want to define a function that takes 3 parameters and returns a list. The parameters will be of any type and then other two parameters are of lists of any types. This is an example...

fun func x [y] [z] = [x, y, z];

Even though the function evaluates to the proper data types, I get a match non-exhaustive warning.

In this example, I don't get the same warning...

fun func x y = (y, x);

It should be because of the lists but I'm not sure how to handle it so I don't actually see a warning.

cpd1
  • 777
  • 11
  • 31

1 Answers1

1

You are getting that warning because you have only told your function what to do when passed an element and two lists, where each of the lists has only 1-element. What happens if one or both of those lists have fewer than 1 or more than 1 element?

Are you familiar with @? It might help you write the function that you seem intent on writing, but without that warning.

John Coleman
  • 51,337
  • 7
  • 54
  • 119
  • I was thinking that it would be doable with just one line but sounds like I need a few checks then for nils? As for @, I am using it but for my main function – cpd1 Nov 16 '15 at 00:21
  • Yay I got it! Thanks for the help! – cpd1 Nov 16 '15 at 00:24
  • Oh I have two lines actually. Not sure how to do it with one hmm. my first line is "x nil nil = [x]" - now, I'm not so sure – cpd1 Nov 16 '15 at 00:26
  • 1
    @cpd1 Hint1: `@` knows how to handle `nil` with no need for special cases. Hint2: you don't *need* to use patterns when defining a function on lists -- identifiers can refer to lists directly. Usually the pattern matching style is the cleanest, but there are exceptions. – John Coleman Nov 16 '15 at 00:54
  • Thank you! I didn't realize that was the case with @. Now it doesn't give the warning and data types are exactly as I need them. I'm not so sure what you mean by hint #2 though. You've helped enough so up to you if you want to clarify. Appreciate it :) – cpd1 Nov 16 '15 at 00:57
  • I assume that by now you have a function which merges the two lists and sticks the element at the front. For fun, see if you can modify the definition so that it sticks the element *between* the two lists. This is actually more motivated since, e.g. `f -1 [3,7,4] [2,5,6] = [3,7,4,-1,2,5,6]` can be thought of as a way of appending two lists in such a way that there is a delimiter between the elements of the two original lists. This sort of thing could be directly useful in e.g. char lists. – John Coleman Nov 16 '15 at 01:07