Complexity Shock Horror II: the Sequel (was Re: MD5 in erlang

Raimo Niskanen raimo.niskanen@REDACTED
Tue Apr 1 14:35:23 CEST 2003


Alright then, I will add integer_to_list(Integer, Base) and 
list_to_integer(List, Base) to the module 'erlang', and to the 
documentation for R9C. We will see if they become real BIFs to R9C.

integer(Base), 2 =< Base, Base =< 36.

integer_to_list/2 allows leading '-' or '+' but no prefix. Both 
uppercase or lowercase digits. No internal '_' since integer_to_list/1 
does not allow it, and neither do the compiler. (yet :-)

list_to_integer/2 produces uppercase digits and a leading '-' for 
negative numbers.

/ Raimo Niskanen, Erlang/OTP



integer_to_list(I, Base)
   when integer(I), integer(Base), Base >= 2, Base =< 1+$Z-$A+10 ->
     if I < 0 ->
	    [$-|integer_to_list(-I, Base, [])];
        true ->
	    integer_to_list(I, Base, [])
     end;
integer_to_list(I, Base) ->
     erlang:fault(badarg, [I, Base]).

integer_to_list(I0, Base, R0) ->
     D = I0 rem Base,
     I1 = I0 div Base,
     R1 = if D >= 10 ->
		 [D-10+$A|R0];
	    true ->
		 [D+$0|R0]
	 end,
     if I1 == 0 ->
	    R1;
        true ->
	    integer_to_list(I1, Base, R1)
     end.



list_to_integer(L, Base)
   when list(L), integer(Base), Base >= 2, Base =< 1+$Z-$A+10 ->
     case list_to_integer_sign(L, Base) of
	I when integer(I) ->
	    I;
	Fault ->
	    erlang:fault(Fault, [L,Base])
     end;
list_to_integer(L, Base) ->
     erlang:fault(badarg, [L,Base]).

list_to_integer_sign([$-|[_|_]=L], Base) ->
     case list_to_integer(L, Base, 0) of
	I when integer(I) ->
	    -I;
	I ->
	    I
     end;
list_to_integer_sign([$+|[_|_]=L], Base) ->
     list_to_integer(L, Base, 0);
list_to_integer_sign([_|_]=L, Base) ->
     list_to_integer(L, Base, 0);
list_to_integer_sign(_, _) ->
     badarg.

list_to_integer([D|L], Base, I)
   when integer(D), D >= $0, D =< $9, D < Base+$0 ->
     list_to_integer(L, Base, I*Base + D-$0);
list_to_integer([D|L], Base, I)
   when integer(D), D >= $A, D < Base+$A-10 ->
     list_to_integer(L, Base, I*Base + D-$A+10);
list_to_integer([D|L], Base, I)
   when integer(D), D >= $a, D < Base+$a-10 ->
     list_to_integer(L, Base, I*Base + D-$a+10);
list_to_integer([], _, I) ->
     I;
list_to_integer(_, _, _) ->
     badarg.



Tony Rogvall wrote:
> Raimo Niskanen wrote:
> 
>>
>> Some comments and some problems. Since Erlang supports the 
>> Base#Integer syntax i think that Base in:
>>     integer_to_list(Integer, Base)
>> should be 2..16, not atoms.
>>
> I just thought it would look nice, but I can buy 2..16 (btw do you know 
> any one using base 13 for output? and since we only alow 2..16 its kind 
> of crippled anyway)
> 
> 
>> The same for:
>>     list_to_integer(List, Base)
>>
> Oh well.
> 
>> But what should happen if you call list_to_integer("16#1f", 8), and 
>> how should you specify any base. I guess the answers are: badarg on 
>> the first and list_to_integer("16#1f", undefined) on the second question.
>>
> Like you said, do NOT allow base syntax in the list_to_integer/2 (why 
> should you?) but maybe in list_to_integer/1 (if that does not break 
> anything, it could!)
> 
>> And to add the prefix in the Right(tm) way is a bit awkward:
>>     List = case integer_to_list(Integer, 16) of
>>             [$-|L] -> "-16#"++L;
>>             L -> "16#"++L
>>         end
> 
> 
> My oppinon is that we ignore the prefix stuff completly just to get 
> things on it's way. I myself have never sent any output in base syntax 
> (anyone?).
> 
> /Tony




More information about the erlang-questions mailing list