Sunday, May 4, 2014

Find count of elements and indexes of a given value of a list in Erlang (using recursiveness)

-module(list_index).
-export([start/2,start_2/2]).

start(Index,[Head|Tail]) ->
    list_value(Index,[Head|Tail],1).

list_value(Index,[Head|Tail],Count) ->
    if
        Index == Count ->
            io:format("Index value is ~p ~n",[Head]);
        true ->
            list_value(Index,Tail,Count+1)
    end.


start_2(Value,[Head|Tail]) ->
    list_value_index(Value,[Head|Tail],1).

list_value_index(Value,[Head|Tail],Count) ->
    if
        Value == Head ->
            io:format("Count is ~p ~n",[Count]);
        true ->
            list_value_index(Value,Tail,Count+1)
    end;

list_value_index(Value,[],Count) ->
    error.

Labels: , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home