Which built-in function makes [H|T] ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

Which built-in function makes [H|T] ?

Zhongzheng Liu
Hi mail-list:

Which built-in function work as   fun(H, T) -> [H|T] end ?

i.e how to fill the following expression without user defined function ?

[H|T] = erlang:apply(???, ???, [H, T]).


Thanks

                           Liu zhongzheng
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Miguel Emilio Ruiz
Hi,

I don't know if what you want is this, I'm newbie in Erlang:

[H|T] = [H] ++ T.


Miguel Ruiz

> El 14 nov 2017, a las 8:56, Zhongzheng Liu <[hidden email]> escribió:
>
> Hi mail-list:
>
> Which built-in function work as   fun(H, T) -> [H|T] end ?
>
> i.e how to fill the following expression without user defined function ?
>
> [H|T] = erlang:apply(???, ???, [H, T]).
>
>
> Thanks
>
>                           Liu zhongzheng
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Richard Carlsson-3
In reply to this post by Zhongzheng Liu
Most operators have a corresponding function in the 'erlang' module. For example, A + B can be written erlang:'+'(A,B). However, the cons operator [|] does not, as far as I know.




        /Richard

2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
Hi mail-list:

Which built-in function work as   fun(H, T) -> [H|T] end ?

i.e how to fill the following expression without user defined function ?

[H|T] = erlang:apply(???, ???, [H, T]).


Thanks

                           Liu zhongzheng
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Roger Lipscombe-2
See this thread from earlier this year:
http://erlang.org/pipermail/erlang-questions/2017-January/091560.html

On 14 November 2017 at 08:50, Richard Carlsson
<[hidden email]> wrote:

> Most operators have a corresponding function in the 'erlang' module. For
> example, A + B can be written erlang:'+'(A,B). However, the cons operator
> [|] does not, as far as I know.
>
>
>
>
>         /Richard
>
> 2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
>>
>> Hi mail-list:
>>
>> Which built-in function work as   fun(H, T) -> [H|T] end ?
>>
>> i.e how to fill the following expression without user defined function ?
>>
>> [H|T] = erlang:apply(???, ???, [H, T]).
>>
>>
>> Thanks
>>
>>                            Liu zhongzheng
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Pierre Fenoll-2
It would be nice to have a cons function, either in the erlang or lists module. 
One usage for it I have had is as the accumulator function of filelib:fold_dirs/5

On Tue 14 Nov 2017 at 10:55, Roger Lipscombe <[hidden email]> wrote:
See this thread from earlier this year:
http://erlang.org/pipermail/erlang-questions/2017-January/091560.html

On 14 November 2017 at 08:50, Richard Carlsson
<[hidden email]> wrote:
> Most operators have a corresponding function in the 'erlang' module. For
> example, A + B can be written erlang:'+'(A,B). However, the cons operator
> [|] does not, as far as I know.
>
>
>
>
>         /Richard
>
> 2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
>>
>> Hi mail-list:
>>
>> Which built-in function work as   fun(H, T) -> [H|T] end ?
>>
>> i.e how to fill the following expression without user defined function ?
>>
>> [H|T] = erlang:apply(???, ???, [H, T]).
>>
>>
>> Thanks
>>
>>                            Liu zhongzheng
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
--

Cheers,
-- 
Pierre Fenoll


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Dmytro Lytovchenko
Operation [H|T] and [H] ++ [T] probably too, is replaced by the compiler with a `put_list` instruction. If you want it to be callable for high order functions it must be located inside some fun, so give it a lambda: fun(H, T) -> [H|T] end, or create your own function,
which will conveniently compile into:

func_info some_name 2 (the arity)
put_list X0 X1 X0
return

or something like this. And it will become usable in HOFs now

2017-11-14 11:04 GMT+01:00 Pierre Fenoll <[hidden email]>:
It would be nice to have a cons function, either in the erlang or lists module. 
One usage for it I have had is as the accumulator function of filelib:fold_dirs/5

On Tue 14 Nov 2017 at 10:55, Roger Lipscombe <[hidden email]> wrote:
See this thread from earlier this year:
http://erlang.org/pipermail/erlang-questions/2017-January/091560.html

On 14 November 2017 at 08:50, Richard Carlsson
<[hidden email]> wrote:
> Most operators have a corresponding function in the 'erlang' module. For
> example, A + B can be written erlang:'+'(A,B). However, the cons operator
> [|] does not, as far as I know.
>
>
>
>
>         /Richard
>
> 2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
>>
>> Hi mail-list:
>>
>> Which built-in function work as   fun(H, T) -> [H|T] end ?
>>
>> i.e how to fill the following expression without user defined function ?
>>
>> [H|T] = erlang:apply(???, ???, [H, T]).
>>
>>
>> Thanks
>>
>>                            Liu zhongzheng
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
--

Cheers,
-- 
Pierre Fenoll


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Cons T Åhs
In reply to this post by Richard Carlsson-3

On 14 Nov 2017, at 09:50, Richard Carlsson <[hidden email]> wrote:

Most operators have a corresponding function in the 'erlang' module. For example, A + B can be written erlang:'+'(A,B). However, the cons operator [|] does not, as far as I know.

I’ve always considered this a gross oversight for several reasons.  One is that you have to write your own local fun when you want to pass it as higher order functional argument, another is that should one generate Erlang code directly one has to take care of this and generate special syntax.  Then there is the personal obvious reason :-)

Cons





        /Richard

2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
Hi mail-list:

Which built-in function work as   fun(H, T) -> [H|T] end ?

i.e how to fill the following expression without user defined function ?

[H|T] = erlang:apply(???, ???, [H, T]).


Thanks

                           Liu zhongzheng
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Richard Carlsson-3
I'll make sure it gets named erlang:'[_|_]'/2 in your honour.

        /Richard

2017-11-14 11:15 GMT+01:00 Cons T Åhs <[hidden email]>:

On 14 Nov 2017, at 09:50, Richard Carlsson <[hidden email]> wrote:

Most operators have a corresponding function in the 'erlang' module. For example, A + B can be written erlang:'+'(A,B). However, the cons operator [|] does not, as far as I know.

I’ve always considered this a gross oversight for several reasons.  One is that you have to write your own local fun when you want to pass it as higher order functional argument, another is that should one generate Erlang code directly one has to take care of this and generate special syntax.  Then there is the personal obvious reason :-)

Cons





        /Richard

2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
Hi mail-list:

Which built-in function work as   fun(H, T) -> [H|T] end ?

i.e how to fill the following expression without user defined function ?

[H|T] = erlang:apply(???, ???, [H, T]).


Thanks

                           Liu zhongzheng
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Richard A. O'Keefe-2
In reply to this post by Richard Carlsson-3


On 14/11/17 9:50 PM, Richard Carlsson wrote:
> Most operators have a corresponding function in the 'erlang' module. For
> example, A + B can be written erlang:'+'(A,B). However, the cons
> operator [|] does not, as far as I know.

I suggest rolling your own.

cons(H, T) -> [H|T].

erlang:apply(?MODULE, cons, [H,T])



_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Richard A. O'Keefe-2
In reply to this post by Roger Lipscombe-2
On reflection, the question needs to be asked:
"why do you want to do this"?
It's not wrong or stupid or anything, it just seems
like a rather low-level thing to be passing around.
I'd normally expect a function parameter to be a
bit more, well, application-specific.
What I'm getting at is that there might be a better
way to do whatever it is you really want to do.

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Zhongzheng Liu
In reply to this post by Cons T Åhs
As far as I know, there is no constructor function for list or tuple.

We have maps:new(), why not lists:new() or tuple:new() ?

                             Liu Zhongzheng


2017-11-14 18:15 GMT+08:00 Cons T Åhs <[hidden email]>:

>
> On 14 Nov 2017, at 09:50, Richard Carlsson <[hidden email]>
> wrote:
>
> Most operators have a corresponding function in the 'erlang' module. For
> example, A + B can be written erlang:'+'(A,B). However, the cons operator
> [|] does not, as far as I know.
>
>
> I’ve always considered this a gross oversight for several reasons.  One is
> that you have to write your own local fun when you want to pass it as higher
> order functional argument, another is that should one generate Erlang code
> directly one has to take care of this and generate special syntax.  Then
> there is the personal obvious reason :-)
>
> Cons
>
>
>
>
>
>         /Richard
>
> 2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
>>
>> Hi mail-list:
>>
>> Which built-in function work as   fun(H, T) -> [H|T] end ?
>>
>> i.e how to fill the following expression without user defined function ?
>>
>> [H|T] = erlang:apply(???, ???, [H, T]).
>>
>>
>> Thanks
>>
>>                            Liu zhongzheng
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: Which built-in function makes [H|T] ?

Richard Carlsson-3
If you want to think in terms of constructors, where you first get an initial value and then "populate" it, then the constructor for lists is written [] (the empty list), and for a tuple you can call erlang:make_tuple(Arity, InitialValue) or use erlang:list_to_tuple(Elements). The function maps:new() simply returns #{}, so why it exists at all is a bit of a mystery. Maybe there were plans to add a maps:new(Options) which could allow you to set special flags, like in array:new(Options).

        /Richard

2017-11-16 3:19 GMT+01:00 Zhongzheng Liu <[hidden email]>:
As far as I know, there is no constructor function for list or tuple.

We have maps:new(), why not lists:new() or tuple:new() ?

                             Liu Zhongzheng


2017-11-14 18:15 GMT+08:00 Cons T Åhs <[hidden email]>:
>
> On 14 Nov 2017, at 09:50, Richard Carlsson <[hidden email]>
> wrote:
>
> Most operators have a corresponding function in the 'erlang' module. For
> example, A + B can be written erlang:'+'(A,B). However, the cons operator
> [|] does not, as far as I know.
>
>
> I’ve always considered this a gross oversight for several reasons.  One is
> that you have to write your own local fun when you want to pass it as higher
> order functional argument, another is that should one generate Erlang code
> directly one has to take care of this and generate special syntax.  Then
> there is the personal obvious reason :-)
>
> Cons
>
>
>
>
>
>         /Richard
>
> 2017-11-14 8:56 GMT+01:00 Zhongzheng Liu <[hidden email]>:
>>
>> Hi mail-list:
>>
>> Which built-in function work as   fun(H, T) -> [H|T] end ?
>>
>> i.e how to fill the following expression without user defined function ?
>>
>> [H|T] = erlang:apply(???, ???, [H, T]).
>>
>>
>> Thanks
>>
>>                            Liu zhongzheng
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://erlang.org/mailman/listinfo/erlang-questions
>
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions