[erlang-questions] List comprehension puzzler
Richard A. O'Keefe
Mon Sep 19 02:51:46 CEST 2016
On 19/09/16 4:56 AM, wrote:
> 4> S = "123a456".
The documentation is very clear:
Erlang reference manual section 3.12 Strings
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
You entered a list of integers using string syntax.
The shell printed that list of integers using string syntax.
> 5> is_integer(S).
Why would you ever have thought that S *might* be an integer?
If you want a hexadecimal integer, section 3.2 is clear.
is an integer in hex.
> 6> [is_integer(I) || I <- S].
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?
?- S = "123a456".
S = [49, 50, 51, 97, 52, 53, 54]
?- S = "123a456", member(X, S), \+ integer(X).
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.)
More information about the erlang-questions