[erlang-questions] Simple output questions

Matthias Lang matthias@REDACTED
Wed Feb 21 09:08:29 CET 2007


Josh Kupershmidt writes:

 > For example, I have a function 'range' defined as:
 > 
 > range(N, N) ->
 >         [N];
 > 
 > range(Min, Max) ->
 >         [Min | range(Min+1, Max)].
 > 
 > If I try to output integers between 1 and 100, the output gets chopped
 > off at 29, ending with " 29|...] " .
 > 
 > Another random question, is it possible to change the number of
 > floating point significant digits. Mine seems to be set to 6. For
 > example
 > 
 > math:sqrt(2).
 > 
 > Gives me "1.41421" . Any way to get more precision? Sorry if these
 > questions are rather simple, I googled a bit without finding anything.

It takes a while to learn your way around the standad libraries. 
Your 'range' function is provided in the 'lists' module:

  6> lists:seq(1,5).  
  [1,2,3,4,5]

The reason your outputs are 'shorter' than you want is just the
default settings for output used by the shell. If you use the 'io'
module, you get more control, e.g.

  7> io:fwrite("~w", [lists:seq(1,100)]).
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]ok

  12> io:fwrite("~.9f\n", [math:sqrt(2)]).
  1.414213562

  13> io:fwrite("~.20f\n", [math:sqrt(2)]). 
  1.41421356237309514547

You can read more about the 'io' module here:

  http://erlang.org/doc/doc-5.5.3/lib/stdlib-1.14.3/doc/html/io.html

and the other modules here:

  http://erlang.org/doc/doc-5.5.3/doc/man_index.html

most of those modules are for specific purposes, so the list isn't
quite as daunting is it may first appear. If you're looking to get
started, try lists, io, string, file, dict and gen_tcp.

Matthias



More information about the erlang-questions mailing list