What is the return type of a tail-recursive function

classic Classic list List threaded Threaded
4 messages Options
by
Reply | Threaded
Open this post in threaded view
|

What is the return type of a tail-recursive function

by
Hi,

What is the return type of a tail-recursive function?

I guess it is a fun(), am I right? (actually I doubt it, but have no idea what the type is)

A strange loop :-)

Please see example as below:
====
-module(test).

-export([start/0]).
-export([loop/0]).

-spec start() -> pid().

start() ->
    spawn(fun() -> loop() end).

-spec loop() -> fun().

loop() ->
    receive
        Any ->
            Any,
            loop()
    end.
====

Kind Regards,
Yao
Reply | Threaded
Open this post in threaded view
|

Re: What is the return type of a tail-recursive function

Loïc Hoguin-3
None, it doesn't return.

You can use no_return() for these cases.

-spec loop() -> no_return().

You would do the same for functions that always throw an exception since
they don't return either.

Cheers,

On 13/02/2020 07:21, by wrote:

> Hi,
>
> What is the return type of a tail-recursive function?
>
> I guess it is a fun(), am I right? (actually I doubt it, but have no
> idea what the type is)
>
> A strange loop :-)
>
> Please see example as below:
> ====
> -module(test).
>
> -export([start/0]).
> -export([loop/0]).
>
> -specstart() ->pid().
>
> start() ->
> spawn(fun() ->loop() end).
>
> -specloop() ->fun().
>
> loop() ->
> receive
> Any->
> Any,
> loop()
> end.
> ====
>
> Kind Regards,
> Yao

--
Loïc Hoguin
https://ninenines.eu
by
Reply | Threaded
Open this post in threaded view
|

Re: What is the return type of a tail-recursive function

by
Yes, no_return() should be the correct answer!

Kind Regards,
Yao

> 在 2020年2月13日,15:21,Loïc Hoguin <[hidden email]> 写道:
>
> None, it doesn't return.
>
> You can use no_return() for these cases.
>
> -spec loop() -> no_return().
>
> You would do the same for functions that always throw an exception since they don't return either.
>
> Cheers,
>
> On 13/02/2020 07:21, by wrote:
>> Hi,
>> What is the return type of a tail-recursive function?
>> I guess it is a fun(), am I right? (actually I doubt it, but have no idea what the type is)
>> A strange loop :-)
>> Please see example as below:
>> ====
>> -module(test).
>> -export([start/0]).
>> -export([loop/0]).
>> -specstart() ->pid().
>> start() ->
>> spawn(fun() ->loop() end).
>> -specloop() ->fun().
>> loop() ->
>> receive
>> Any->
>> Any,
>> loop()
>> end.
>> ====
>> Kind Regards,
>> Yao
>
> --
> Loïc Hoguin
> https://ninenines.eu

Reply | Threaded
Open this post in threaded view
|

Re: What is the return type of a tail-recursive function

zxq9-2
In reply to this post by by
On 2020/02/13 15:21, by wrote:
> What is the return type of a tail-recursive function?

This is actually a more involved question than the way it was posed.

Briefly, it is whatever type is actually returned.

An infinite loop or loop-until-termination function would be typed as
no_return() as mentioned already.

Consider a main loop or service loop:


   -spec loop(State) -> no_return()
       when State :: term().

   loop(State) ->
       receive
           {expected, Message} ->
               NewState = do_stuff(Message, State),
               loop(NewState);
           retire ->
               exit(normal)
       end.


A recursive function that has a clause that breaks the loop, however,
will have a normal return type.

Consider a manually written "sum" function:


   -spec sum([number()]) -> number().
   % sum/2 actually *does* return though it is tail-recursive.

   sum(Numbers) ->
       sum(Numbers, 0).

   sum([H | T], A) ->
       sum(T, H + A);
   sum([], A) ->
       A.


If you have a function that *might* throw an exception then you
disregard that possibility and type it the way it is coded.

Exceptions are EXCEPTIONAL and should not be used as a hacky way to
achieve a non-local return.
Consider, for example, that every function ever written *might* return
an exception, but we don't type every function as `some_type() |
no_return()` do we?

-Craig