[erlang-questions] List comprehension puzzler

John R. Ashmun john.ashmun@REDACTED
Mon Sep 19 03:34:54 CEST 2016


What, no APL?

Kudos, master, for your restraint on the all-caps front.  I sincerely
appreciate that.

On Sun, Sep 18, 2016 at 5:51 PM, Richard A. O'Keefe <ok@REDACTED>
wrote:

>
>
> On 19/09/16 4:56 AM, lloyd@REDACTED wrote:
>
>> 4> S = "123a456".
>> "123a456"
>>
>
> The documentation is very clear:
>
> Erlang reference manual section 3.12 Strings
> http://erlang.org/doc/reference_manual/data_types.html#id71049
>
>     Strings are enclosed in double quotes ("),
>     but is(sic.) not a data type in Erlang.
>     Instead, a string "hello" is shorthand for the list
>     [$h,$e,$l,$l,$o], that is, [104,101,108,108,111].
>
> When the shell sees a list that might have been a string,
> it prints it as a string.  So
>
>   > [65,66,67].
>   "ABC"
>
> You entered a list of integers using string syntax.
> The shell printed that list of integers using string syntax.
>
> 5> is_integer(S).
>> false
>>
>
> Why would you ever have thought that S *might* be an integer?
> If you want a hexadecimal integer, section 3.2 is clear.
>
>     16#123a456
>
> is an integer in hex.
>
>>
>> 6> [is_integer(I) || I <- S].
>> [true,true,true,true,true,true,true]
>>
>
> So you did know that S is a list.
>
>>
>> Please tell me what I don't understand.
>>
>
> That would require us to read your mind.  What did you expect
> to happen and why did you expect that?
>
> Here's Prolog:
>
> ?- S = "123a456".
>
> S = [49, 50, 51, 97, 52, 53, 54]
>
> Yes
> ?- S = "123a456", member(X, S), \+ integer(X).
>
> No
>
> Here's Haskell:
> Prelude> :type "123a456"
> "123a456" :: [Char]
>
> (That is, "123a456" is a list of characters.  Char and Int are
> distinct in Haskell, not in Erlang or Prolog.)
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160918/14201420/attachment.htm>


More information about the erlang-questions mailing list