Testing for improper list

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

Testing for improper list

Karlo Kuna
Hello everyone,

Is there any built in method of checking if list is improper?

Thanks

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

Re: Testing for improper list

Guilherme Andrade
Hello Karlo,

On 16 July 2018 at 01:07, Karlo Kuna <[hidden email]> wrote:
Is there any built in method of checking if list is improper?

Something like this should do:

> is_improper_list(Value) when length(Value) >= 0 ->                                                                                                                                                                                            
>     false;                                                                                                                                                                                                                                    
> is_improper_list(Value) ->                                                                                                                                                                                                                    
>     is_list(Value).

`length/1' will fail upon Value being an improper list.

--
Guilherme

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

Re: Testing for improper list

Guilherme Andrade
(I now notice I misread your question; I don't believe there is any built-in function for it, no.)

--
Guilherme

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

Re: Testing for improper list

黃耀賢 (Yau-Hsien Huang)
In reply to this post by Guilherme Andrade
As off topic, the is_improper_list/1 may be rewritten to ...

is_improper_list([_|Value]) -> is_improper_list(Value);

On Mon, Jul 16, 2018 at 8:17 AM, Guilherme Andrade <[hidden email]> wrote:
Hello Karlo,

On 16 July 2018 at 01:07, Karlo Kuna <[hidden email]> wrote:
Is there any built in method of checking if list is improper?

Something like this should do:

> is_improper_list(Value) when length(Value) >= 0 ->                                                                                                                                                                                            
>     false;                                                                                                                                                                                                                                    
> is_improper_list(Value) ->                                                                                                                                                                                                                    
>     is_list(Value).

`length/1' will fail upon Value being an improper list.

--
Guilherme

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




--

Best Regards.

--- Y-H. H.


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

Re: Testing for improper list

黃耀賢 (Yau-Hsien Huang)
Yes, Richard, I gave the name because of language issue that I made the is_improper_list/1 working but not be aware of its literal meaning, when I reconsidered Guilherme's code.

The reason to consider a built-in may be that it won't work when applying length/1 to a value [a|b], getting a bad_arg error. And is_list/1 does not determine whether a value is an improper list.

Then there shell be an augmented version of is_list/1, that ...

is_improper_list([]) -> false;
is_improper_list([_|Value]) -> is_improper_list(Value);
is_improper_list(_) -> true.

Though the good use is to avoid writing some code to put any improper list to your system.


On Mon, Jul 16, 2018 at 8:51 PM, Richard O'Keefe <[hidden email]> wrote:
It is not entirely clear what you mean by
an improper list.  Presumably [a|b] is an
improper list, but is b all by itself an
improper list, or what?

I'd be tempted to go with
is_not_a_proper_list(X) when length(X) >= 0 -> false;
is_not_a_proper_list(_) -> true.
but more importantly,

- why do you want a built-in (as opposed to a library
  function)?
- why do you want this at all?



On 16 July 2018 at 14:37, 黃耀賢 (Yau-Hsien Huang) <[hidden email]> wrote:
As off topic, the is_improper_list/1 may be rewritten to ...

is_improper_list([_|Value]) -> is_improper_list(Value);

On Mon, Jul 16, 2018 at 8:17 AM, Guilherme Andrade <[hidden email]> wrote:
Hello Karlo,

On 16 July 2018 at 01:07, Karlo Kuna <[hidden email]> wrote:
Is there any built in method of checking if list is improper?

Something like this should do:

> is_improper_list(Value) when length(Value) >= 0 ->                                                                                                                                                                                            
>     false;                                                                                                                                                                                                                                    
> is_improper_list(Value) ->                                                                                                                                                                                                                    
>     is_list(Value).

`length/1' will fail upon Value being an improper list.

--
Guilherme

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




--

Best Regards.

--- Y-H. H.


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





--

Best Regards.

--- Y-H. H.


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

Re: Testing for improper list

Karlo Kuna
i have solution just as Yau-Hsien suggested but i was wandering is there highly optimized version (bif of nif) of this. 
sure one can easily do NIF but i think is_improper_list/1 or is_proper_list/1 should be in standard library. 

improper list is pretty clear term in erlang documentation. 

also why i want this: suppose i want type ob object to be list but i also want some sort of additional information for it, 
then syntax [...|some_type_or_property] seems elegant. 
Alternatives are
  • {some_type_or_property, List}  
  • parse transformations
  • record or map
however as i need/want this as input to file:consult parse transformations are difficult here and object wrappers as 
tuple, map or record are to verbose and noisy 




On Mon, Jul 16, 2018 at 4:17 PM 黃耀賢 (Yau-Hsien Huang) <[hidden email]> wrote:
Yes, Richard, I gave the name because of language issue that I made the is_improper_list/1 working but not be aware of its literal meaning, when I reconsidered Guilherme's code.

The reason to consider a built-in may be that it won't work when applying length/1 to a value [a|b], getting a bad_arg error. And is_list/1 does not determine whether a value is an improper list.

Then there shell be an augmented version of is_list/1, that ...

is_improper_list([]) -> false;
is_improper_list([_|Value]) -> is_improper_list(Value);
is_improper_list(_) -> true.

Though the good use is to avoid writing some code to put any improper list to your system.


On Mon, Jul 16, 2018 at 8:51 PM, Richard O'Keefe <[hidden email]> wrote:
It is not entirely clear what you mean by
an improper list.  Presumably [a|b] is an
improper list, but is b all by itself an
improper list, or what?

I'd be tempted to go with
is_not_a_proper_list(X) when length(X) >= 0 -> false;
is_not_a_proper_list(_) -> true.
but more importantly,

- why do you want a built-in (as opposed to a library
  function)?
- why do you want this at all?



On 16 July 2018 at 14:37, 黃耀賢 (Yau-Hsien Huang) <[hidden email]> wrote:
As off topic, the is_improper_list/1 may be rewritten to ...

is_improper_list([_|Value]) -> is_improper_list(Value);

On Mon, Jul 16, 2018 at 8:17 AM, Guilherme Andrade <[hidden email]> wrote:
Hello Karlo,

On 16 July 2018 at 01:07, Karlo Kuna <[hidden email]> wrote:
Is there any built in method of checking if list is improper?

Something like this should do:

> is_improper_list(Value) when length(Value) >= 0 ->                                                                                                                                                                                            
>     false;                                                                                                                                                                                                                                    
> is_improper_list(Value) ->                                                                                                                                                                                                                    
>     is_list(Value).

`length/1' will fail upon Value being an improper list.

--
Guilherme

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




--

Best Regards.

--- Y-H. H.


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





--

Best Regards.

--- Y-H. H.

_______________________________________________
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