4

I'm trying to learn Erlang, coming from a C++/Java background. This forces me to re-think all my methods.

Right now I'm trying to write something that returns the N first elements of a list. Right now it looks like this, although I can't call functions in guards or if expressions. What is the Erlang way of doing this?

take([Xh|Xr],N,Xn) ->
    if
        len(Xn) /= N -> take(Xr,N,app(Xh, Xn));
        len(Xn) == N -> Xn
    end.

I also tried calling the function before, but that didn't work either:

take([Xh|Xr],N,Xn) ->
   G = len(Xn);
    if
        G /= N -> take(Xr,N,app(Xh, Xn));
        G == N -> Xn
    end.
1
  • 3
    This question is virtually the same as this question which has some more explicit answers.
    – rvirding
    Commented Oct 29, 2011 at 19:40

3 Answers 3

5

Generally with this kind of problems, you need to switch to a recursive way of thinking instead of the iterative approach you're using. Here's what I would do:

take(List, N) ->
    take(List, N, []).
take(_List, 0, Acc) ->
    lists:reverse(Acc);
take([H|T], N, Acc) ->
    take(T, N - 1, [H|Acc]).

It's really common for people coming from languages that promote the iterative approach to try and shoehorn that approach into Erlang. The problem is that Erlang doesn't have the primitives for doing it that way since it's a functional language. So you're forced to do it the functional way, and in the end it's often the more elegant approach.

2
  • I thought this WAS a recursive way of solving it, only with an added condition. Well, I guess I haven't got into the thinking yet. Thanks!
    – Rickard
    Commented Oct 29, 2011 at 20:13
  • 1
    Well it is recursive in that you're calling yourself, but you're counting up instead of down, which is why I'm calling it iterative. This makes it harder to determine when you've reached your base case.
    – Fylke
    Commented Oct 30, 2011 at 10:59
4

In addition to Fylke's solution, there is also something to be said for a body recursive approach:

take(_List,0) ->
  [];
take([H|T],N) ->
  [H|take(T,N-1)].
3
  • I know that the '_' can mark an irrelevant variable, but what does it do in this sense, as in '_List'?
    – Rickard
    Commented Oct 29, 2011 at 20:54
  • 3
    It marks a named irrelevant variable. The point is that you can hint the reader that we take a list at that position rather than just something. The Erlang internals treat them the same way. Commented Oct 29, 2011 at 23:59
  • @Rickard It's usually called the don't care variable as it is definitely not irrelevant, as it must be there, but we don't care what value it has. The _ variable is never bound to a value so it can be used multiple times in a pattern without significance. Normally the compiler will issue a warning when you just bind a variable with out using it, however if the variable starts with an '_' as in _List above it issues no such warning. NOTE such a variable is a normal variable and behaves in every way as one, only _ is really special in the language.
    – rvirding
    Commented Oct 30, 2011 at 2:59
0

Your approach isn't wrong per se, it just needs a bit of help:

-module(foo).
-compile(export_all).

take([Xh|Xr],N,Xn) ->
    G = length(Xn), %% This line had trouble. Use length/1 and end with , not ;
    if
        G /= N ->
          take(Xr,N,app(Xh, Xn));
        G == N ->
          Xn
    end.

app(X, L) ->
    L ++ [X].

As other people hints, your approach is not very Erlang idiomatic, and the other solutions are far better. Also, look up the source code for lists:split/2

https://github.com/erlang/otp/blob/master/lib/stdlib/src/lists.erl#L1351

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.