Erlang/OTP Forums

Author Message

<  Erlang  ~  problem with tail recursion

ishmael
Posted: Fri Oct 10, 2008 7:27 am Reply with quote
Joined: 23 Jul 2008 Posts: 9 Location: RSA
hi,

i am experiencing problems when dealing with recursion, i am writing a code to tell accept messages that have a. timestamp b. messagetype. The objective is for the module to accept the message, evaluate the time of the message to see if it came in between 12am and 1am, 2am and 2am... the problem i have is that i can only evaluate the message for 12am and 1am. I need to evaluate the message for 24hrs.
Code:
-module(timer2).
-export([do/1]). how can i do this?

do(SearchField) ->


 ErlangTime = {{2008,10,06},{0,23,10}},
 
 {Date, Time} = ErlangTime,
 
 
 TweAm    = {0,0,0},
 OneAm    = {0,59,0},
 TwoAm    = {1,59,0},
         
if  SearchField = 'Private'->
 MyFun(TweAm, Time,OneAm);
   {ok, "not private"}             
end. % i get error "syntax error before end"
   
 MyFun(StartTime, Time, EndTime) ->
     if (StartTime < Time) , (Time < EndTime)
                           -> 
     io:format("~p~s~p~n", [SearchField, StartTime]);
     MyFun(OneAm, Time,TwoAm)                     
                                 
                               
                       end.
View user's profile Send private message
rvirding
Posted: Fri Oct 10, 2008 7:54 pm Reply with quote
User Joined: 30 Aug 2006 Posts: 452 Location: Stockholm, Sweden
You have a number of problems here:

- Call the function MyFun for myfun as MyFun is a variable (starts with capital letter) and functiona names must be atoms.

- Any variables defined in a function are local to that function, there are no global variables. So TweAm, OneAm and TwoAm are only visible within do/1 and not myfun/3.

- = is a matching operator and not a test operator and is not allowed in the test part of an if. The if should look like:

if SearchField == 'Private' ->
myfun(TweAm, Time, OneAm);
true -> {ok,"not private"} %Need test here as well
end.

- in myfun/3 you can only use variables defined internally or passed as arguments. As there is not termination test myfun would recurse for ever.

Try reading some of the documentation on erlang.org which will help you with many of the problems here.
View user's profile Send private message Visit poster's website MSN Messenger
ishmael
Posted: Mon Oct 13, 2008 6:35 am Reply with quote
Joined: 23 Jul 2008 Posts: 9 Location: RSA
Thank you!
View user's profile Send private message
ishmael
Posted: Mon Oct 13, 2008 3:15 pm Reply with quote
Joined: 23 Jul 2008 Posts: 9 Location: RSA
Thank you!
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum