I am trying to make a program that will read in a number and then output every digit of that number in a list. However, most of the things look fine until I try with number 8 and 9. The program only output \b
\t
instead.
if the input number contains 8 or 9, and in the same time there are other numbers, for example 283
, it will print normally. Otherwise if there is only 8 or 9, such as8
, 99
, then it will give me that binary representation of 8 and 9 (if I remember correctly).
My program is as below:
digitize(0)-> 0;
digitize(N) when N < 10 -> [N];
digitize(N) when N >= 10 -> digitize(N div 10)++[N rem 10].
Asked
Active
Viewed 711 times
3

maoyi
- 433
- 1
- 6
- 12
-
2You don't need the first clause, and in fact it returns the wrong result if you pass a 0: `digitize(0)` should return `[0]`, but because of that clause it returns `0`. If you drop that clause and keep the other two, you get the right answer. Also, you don't need a guard on the last clause, since the previous `N < 10` already leaves everything greater than or equal to 10 for the final clause. – Steve Vinoski Sep 19 '15 at 17:42
-
@SteveVinoski yup, thanks for the tips! =) – maoyi Sep 19 '15 at 18:09
-
nice, I had the same problem. – Tommy Jan 21 '16 at 18:21
-
Does this answer your question? [Number in list printed as "\f"](https://stackoverflow.com/questions/34857704/number-in-list-printed-as-f) – 2240 Feb 25 '23 at 23:03
2 Answers
3
The function returns the expected list, but the shell shows lists of numbers which are ASCII-codes of characters as strings (because that's just what strings are in Erlang; there's no special string type). You can see it by just entering [8, 8]
(e.g.) at the prompt and disable this behavior by calling shell:strings(false)
(and shell:strings(true)
when you need the normal behavior again).

Alexey Romanov
- 167,066
- 35
- 309
- 487
3
Strings in Erlang are no separate type but a list of numbers. List printing has a heuristic to detect when it might be a string. If it thinks it's a string it will be printed as such. \b is the backspace character and \t is the tab character which are ASCII codes 8 and 9
See also:

Peer Stritzinger
- 8,232
- 2
- 30
- 43