In addition to having the map function available with many arities (up to 4), Prolog allows you (under certain circumstances) to map a multiple arity function onto a single list. Say you want to test whether 'x' is a member of multiple lists. You can do:
maplist(member(x),[[a,b,c,x],[3,f,s,x]]).
The first argument of member is included, and the whole thing is mapped onto the list.
Question 1: Is something similar available to Erlang? I can't find it in the documentation, but then again I can't find it in any Prolog documentation either.
Question 2: How to use map (and similar functions) with multiple arities? Roll your own?
Thanks.
A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.
Pattern matching occurs when evaluating a function call, case- receive- try- expressions and match operator (=) expressions. In a pattern matching, a left-hand side pattern is matched against a right-hand side term. If the matching succeeds, any unbound variables in the pattern become bound.
You can use length() to find the length of a list, and can use list comprehensions to filter your list. num(L) -> length([X || X <- L, X < 1]).
Maybe something like:
23> lists:map(fun(L) -> lists:member(42,L) end, [[1,5,8,4],[13,42],[7,2,10]]).
[false,true,false]
24>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With