Shree
Shree

Reputation: 11

Implement a lists:map using case clauses instead of function clauses in Erlang

Can anyone tell me what this means? I am new to this and my friend recommended me to post in this website. By the way I'm new to Erlang.

If possible I want to write a code in editor and I don't even understand the question any sample input/output and how it works an explanation will do. Thankyou

Upvotes: 1

Views: 180

Answers (3)

7stud
7stud

Reputation: 48599

Here's a simple example showing how to use function clauses, then case statements to do the same thing. Put the following code in a file named a.erl in some directory:

-module(a).
-export([show_stuff/1, show_it/1]).

show_stuff(1) ->
    io:format("The argument was 1~n");
show_stuff(2) ->
    io:format("The argument was 2~n");
show_stuff(_)->
    io:format("The argument was something other than 1 or 2~n").

show_it(X) ->
    case X of
        1 -> io:format("The argument was 1~n");
        2 -> io:format("The argument was 2~n");
        _ -> io:format("The argument was something other than 1 or 2~n")
    end.

Note that the file name, a.erl and the module directive:

-module(a).

must match. So, if you named your file homework1.erl, then the module directive in the file must be:

-module(homework1).

To save a lot of typing, it's best to use very short module names (as you will see below).

In a terminal window, switch directories to the directory containing a.erl:

 ~$ cd erlang_programs/

then launch the erlang shell:

 ~/erlang_programs$ erl
Erlang/OTP 24 [erts-12.0.2] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1]

Eshell V12.0.2  (abort with ^G)

Next, execute the following statements:

1> c(a).   <--- Compiles the code in your file 
{ok,a}     <--- Or, you may get errors which must be corrected, then try recompiling.

2> a:show_stuff(1).
The argument was 1
ok

3> a:show_stuff(4).
The argument was something other than 1 or 2
ok

4> a:show_it(1).
The argument was 1
ok

5> a:show_it(4).
The argument was something other than 1 or 2
ok

6> 

Note the syntax for calling a function defined in a file/module:

 module_name:function_name(arg1, arg2, ... argn).

Upvotes: 1

Alexey Romanov
Alexey Romanov

Reputation: 170745

any sample input/output and how it works an explanation will do

In the documentaion linked in Brujo Benavides's answer, you can see:

Takes a function from As to Bs, and a list of As and produces a list of Bs by applying the function to every element in the list. This function is used to obtain the return values.

So F is a function (of a single argument) such as fun(X) -> X*2 end. See https://www.erlang.org/doc/programming_examples/funs.html#syntax-of-funs or https://www.erlang.org/doc/reference_manual/expressions.html#funs to understand fun expressions. List1 is a list of values which the function F can work on (in this case numbers) such as [1,2,3]. Then list:map(fun(X) -> X*2 end, [1,2,3]) calls fun(X) -> X*2 end on each element of list [1,2,3] and returns the list of return values [2,4,6]. Your function should give the same result on these arguments.

Upvotes: 1

Brujo Benavides
Brujo Benavides

Reputation: 1958

It seems to me that the question refers to the implementation of lists:map/2, a function that applies the same function (received as a parameter) to all elements of a list and returns the resulting list.

In other words, this function.

You can check the OTP Github repo to see how that function is implemented:

map(F, List) when is_function(F, 1) ->
    case List of
        [Hd | Tail] -> [F(Hd) | map_1(F, Tail)];
        [] -> []
    end.

map_1(F, [Hd | Tail]) ->
    [F(Hd) | map_1(F, Tail)];
map_1(_F, []) ->
    [].

Or you can conceive an even simpler implementation, as…

map(F, []) -> [];
map(F, [H|T]) -> [F(H) | map(F, T)].

Both of them (for the OTP version, I'm referring to map_1/2) use pattern-matching in function clause heads to distinguish between the base case and the recursive step of the function.

The request that you received is to implement the same algorithm using a single function clause with a case clause instead of the two function clauses you see above.

Upvotes: 3

Related Questions