Need to print integers

103 views
Skip to first unread message

Developer at Works

unread,
Oct 21, 2010, 11:21:04 AM10/21/10
to erlang-prog...@googlegroups.com
Hello Geeks

Just for fun, I was trying this and thought to share with you guys too. :)

Can we make list of integers, with a sequence while following these instructions:
For ex:
 -> Input: 
      $ myModule:f1(10).
 -> Output:
      $ [1,2,3,4,5,6,7,8,9,10]

Conditions are: (DONT USE FOLLOWING CONCEPTS)
 - Dont use any BIF's
     for ex- lists:seq() etc
 - Dont use Accumulator
     for ex-
          f1(0, Acc) -> lists:reverse(Acc);
          f1(N, Acc) -> f1(N-1, [N|Acc]).
 - Dont use any SERVER e.g. GEN-SERVER etc
 - Use tail-recursion concept only

To conclude, can we make this in any way, while following the above guidelines.

--
Thank you
Abhinav Mehta

Ale

unread,
Oct 21, 2010, 11:36:12 AM10/21/10
to erlang-prog...@googlegroups.com


2010/10/21 Developer at Works <develope...@gmail.com>

Sure you can: http://ideone.com/WfMFe (left a link in case the rest want to try the exercise :-)

Cheers,

 
--
Thank you
Abhinav Mehta

--
Erlang Programming Website:
http://www.erlangprogramming.org/



--
Ale.

Ale

unread,
Oct 21, 2010, 11:42:03 AM10/21/10
to erlang-prog...@googlegroups.com


2010/10/21 Ale <peralta....@gmail.com>



2010/10/21 Developer at Works <develope...@gmail.com>

Hello Geeks

Just for fun, I was trying this and thought to share with you guys too. :)

Can we make list of integers, with a sequence while following these instructions:
For ex:
 -> Input: 
      $ myModule:f1(10).
 -> Output:
      $ [1,2,3,4,5,6,7,8,9,10]

Conditions are: (DONT USE FOLLOWING CONCEPTS)
 - Dont use any BIF's
     for ex- lists:seq() etc
 - Dont use Accumulator
     for ex-
          f1(0, Acc) -> lists:reverse(Acc);
          f1(N, Acc) -> f1(N-1, [N|Acc]).
 - Dont use any SERVER e.g. GEN-SERVER etc
 - Use tail-recursion concept only


Humm... thinking a bit more, do you mean recursion or must it be tail-recursive? If it has to be tail-recursive, how do you manage without an accumulator?

 
To conclude, can we make this in any way, while following the above guidelines.


Sure you can: http://ideone.com/WfMFe (left a link in case the rest want to try the exercise :-)

Cheers,

 
--
Thank you
Abhinav Mehta

--
Erlang Programming Website:
http://www.erlangprogramming.org/



--
Ale.



--
Ale.

Developer at Works

unread,
Oct 21, 2010, 3:31:34 PM10/21/10
to erlang-prog...@googlegroups.com
Its a fantastic-one Ale, nice approach.
I'm stating for tail-recursive, yeah I know that its hard to achieve that, without using accumulator, thats why I already wrote code with using accumulator and looking to know some alternate and better way of doing the same stuff.

--
Abhinav Mehta

Reza Moussavi

unread,
Oct 21, 2010, 5:17:24 PM10/21/10
to Erlang Programming
what about this?
f1(0)->[];
f1(N)->f1(N-1)++[N].

not tail recursive yet, but lets think more

On Oct 21, 5:21 pm, Developer at Works <developeratwo...@gmail.com>
wrote:
> Hello Geeks
>
> Just for fun, I was trying this and thought to share with you guys too. :)
>
> Can we make list of integers, with a sequence while following these
> instructions:
> For ex:
>  -> *Input:*
>       $ myModule:f1(10).
>  -> *Output:*
>       $ [1,2,3,4,5,6,7,8,9,10]
>
> Conditions are: (*DONT USE FOLLOWING CONCEPTS*)
>  - *Dont use any BIF's*
>      for ex- lists:seq() etc
>  - *Dont use Accumulator*
>      for ex-
>           f1(0, Acc) -> lists:reverse(Acc);
>           f1(N, Acc) -> f1(N-1, [N|Acc]).
>  - *Dont use any SERVER* e.g. GEN-SERVER etc
>  - *Use tail-recursion* concept only

Reza Moussavi

unread,
Oct 21, 2010, 5:22:34 PM10/21/10
to Erlang Programming
tail recursive one :
f1(N)->f2(N,[]).
f2(0,Buf)->Buf;
f2(N,Buf)->f2(N-1,[N|Buf]).


On Oct 21, 9:31 pm, Developer at Works <developeratwo...@gmail.com>
wrote:
> Its a fantastic-one Ale, nice approach.
> I'm stating for tail-recursive, yeah I know that its hard to achieve that,
> without using accumulator, thats why I already wrote code with using
> accumulator and looking to know some alternate and better way of doing the
> same stuff.
>
> --
> Abhinav Mehta
>
>
>
>
>
>
>
> On Thu, Oct 21, 2010 at 9:12 PM, Ale <peralta.alejan...@gmail.com> wrote:
>
> > 2010/10/21 Ale <peralta.alejan...@gmail.com>
>
> >> 2010/10/21 Developer at Works <developeratwo...@gmail.com>
>
> >> Hello Geeks
>
> >>> Just for fun, I was trying this and thought to share with you guys too.
> >>> :)
>
> >>> Can we make list of integers, with a sequence while following these
> >>> instructions:
> >>> For ex:
> >>>  -> *Input:*
> >>>       $ myModule:f1(10).
> >>>  -> *Output:*
> >>>       $ [1,2,3,4,5,6,7,8,9,10]
>
> >>> Conditions are: (*DONT USE FOLLOWING CONCEPTS*)
> >>>  - *Dont use any BIF's*
> >>>      for ex- lists:seq() etc
> >>>  - *Dont use Accumulator*
> >>>      for ex-
> >>>           f1(0, Acc) -> lists:reverse(Acc);
> >>>           f1(N, Acc) -> f1(N-1, [N|Acc]).
> >>>  - *Dont use any SERVER* e.g. GEN-SERVER etc
> >>>  - *Use tail-recursion* concept only
>
> > Humm... thinking a bit more, do you mean recursion or must it be
> > tail-recursive? If it has to be tail-recursive, how do you manage without an
> > accumulator?
>
> >> To conclude, can we make this in any way, while following the above
> >>> guidelines.
>
> >> Sure you can:http://ideone.com/WfMFe(left a link in case the rest want

Ale

unread,
Oct 21, 2010, 5:40:45 PM10/21/10
to erlang-prog...@googlegroups.com


2010/10/21 Reza Moussavi <reza.m...@gmail.com>

what about this?
f1(0)->[];
f1(N)->f1(N-1)++[N].

Nice, short and simple

--
Ale.

Chenini, Mohamed

unread,
Oct 21, 2010, 5:46:53 PM10/21/10
to erlang-prog...@googlegroups.com

Hi,

 

I am trying the examples in chapter 11 paragraph: Communication and Messages in page 252

 

But I am getting the error below.

 

(foo@plun2102)1> net_admin:ping('bar@plun0142').

** exception error: undefined function net_admin:ping/1

 

 

Where is the module net_admin located so I can its path to PATH.

This is in Linux.

 

Thanks,

Mohamed

 

Erlang Programming Website:
http://www.erlangprogramming.org/

====================
This email/fax message is for the sole use of the intended
recipient(s) and may contain confidential and privileged information.
Any unauthorized review, use, disclosure or distribution of this
email/fax is prohibited. If you are not the intended recipient, please
destroy all paper and electronic copies of the original message.

Chenini, Mohamed

unread,
Oct 21, 2010, 5:54:21 PM10/21/10
to erlang-prog...@googlegroups.com

Sorry there a typo in the command.

It should be net_adm:ping and not net_admin:ping

 

 

Regards,

Mohamed


Reply all
Reply to author
Forward
0 new messages