Question

Having read current answer THIS I did the following:

A = {1, 99}, B = {5, 15}.
F = fun({key_X, val_X},{key_Y, val_Y}) -> 
       {val_X, key_X} =< {val_Y, key_Y} 
    end.

And then, put it to lists:sort/2 function. As follows:

lists:sort(F, [A, B]).

But got the error exception :

exception error: no function clause matching erl_eval:'-inside-an-interpreted-fun-'({1,99},{5,15})

What is a mistake here? Can you guide me through?

Was it helpful?

Solution

You have to note that Erlang differentiates atoms and identifiers using their case.
Eg:

  • [a, b, bla, key_1, val_X] is a list of atoms
  • [A, B, Bla, Key_1, Val_X] is a list of variables

In your code you defined F so that it behaves a certain way for specific atoms as input.
What you should have done (and what they did in your link) is use variable identifiers:

F = fun({Key_X, Val_X},{Key_Y, Val_Y}) -> {Val_X, Key_X} =< {Val_Y, Key_Y} end.

See?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top