[ERLANG]: Why answer returns list in a list rather than list? -
given module
-module(p1). -export([f2/2]). f2([a, | b]) -> {a, b}; f2([a, b | _]) -> {a, b}; f2([a]) -> a; f2(_) -> no_match.
i asked seek input value
p1:f2([1,1,[1,1]]).
my reply input value matches first function clause of f2, , such gives result:
{1,[1,1]}
but according given reply sheet, reply is
{1,[[1,1]]}
i can't quite head around why list within list, rather list in answer. appreciate explanation this, give thanks you.
you should how head-tail works in erlang. tail list; 1 element list, empty list, list.
lets illustration in shell:
2> fun = fun([h | t]) -> {h, t} end. #fun<erl_eval.6.90072148> 3> fun([a]). {a,[]} 4> fun([a,b]). {a,[b]} 5> fun([a,[a,b]]). {a,[[a,b]]} 6> fun([a,b,c]). {a,[b,c]} 7> fun([a,b,c,d]). {a,[b,c,d]}
so in case returning a
repeted 1
, b
list of remaining elements. in our case 1 element remains, homecoming one-element list. , element two-element list, hence list in list [[1, 1]]
.
list erlang tuples
No comments:
Post a Comment