2

I just started learning Erlang so please bear with me if this question seems a little simple.

Hi guys. I've been thinking about it for a while but nothing I come up with seems to be working.

I am writing an Erlang function that is supposed to take a list as an argument then print the list with my name in front of it. For the purposes of this question, let's say my name is "James".

If I type in testmodule:NameInFront("Legible", "Hey", "Think"). Erlang should return ["James", "Legible", "Hey", "Think"]

This is the code I have so far:

-module(testmodule).

-export([NameInFront/1]).

NameInFront(List)-> ["James"]++[List].

It works just fine when I type in just one word, which I guess it the fault of the NameInFront/1 part but I want it to be able to handle any amount of words I type in. Anyone know how I can get my function to handle multiple inputs? Thank you very much.

G_Man
  • 169
  • 1
  • 2
  • 12

1 Answers1

5

I'm not quite sure what you mean: whether you want your function to be variadic (take a flexible number of arguments), or you are having trouble getting your lists to join together properly.

Variadic functions are not the way Erlang works. FunctionName/Arity defines the concrete identity of a function in Erlang (discussed here). So our way of having a function take multiple arguments is to make one (or more) of the arguments a list:

print_terms(Terms) -> io:format("~tp~n", [Terms]).

The io:format/2 function itself actually takes a list as its second function, which is how it deals with a variable number of arguments:

print_two_things(ThingOne, ThingTwo) ->
     io:format("~tp~n~tp~n", [ThingOne, ThingTwo]).

In your case you want to accept a list of things, add your name to it, and print it out. This is one way to do it.

name_in_front(ListOfStrings) ->
    NewList = ["James" | ListOfStrings],
    io:format("~p~n", [NewList]).

Using the ++ operator is another (which is actually a different syntax for a recursive operation which expands to the exact same thing, ):

name_in_front(ListOfStrings) ->
    NewList = ["James"] ++ ListOfStrings,
    io:format("~tp~n", [NewList]).

But that's a little silly, because it is intended to join two strings together in a simple way, and in this case it makes the syntax look weird.

Yet another way would be to more simply write a function that take two arguments and accomplishes the same thing:

any_name_in_front(Name, ListOfThings) ->
    io:format("~tp~n", [[Name | ListOfThings]]).

The double [[]] is because io:format/2 takes a list as its second argument, and you want to pass a list of one thing (itself a list) into a single format substitution slot (the "~tp" part).

One thing to note is that capitalization matters in Erlang. It has a meaning. Module and function names are atoms, which are not the same thing as variables. For this reason they must be lowercase, and because they must be lowercase to start with the convention is to use underscores between words instead of usingCamelCase. Because, well, erlangIsNotCpp.

Play around in the shell a bit with the simple elements of the function you want, and once you have them ironed out write it into a source file and give it a try.

Community
  • 1
  • 1
zxq9
  • 13,020
  • 1
  • 43
  • 60