[erlang-questions] is this a well written function

Richard A. O'Keefe ok@REDACTED
Thu Feb 12 02:58:48 CET 2015


On 11/02/2015, at 8:11 pm, Martin Koroudjiev <mrtndimitrov@REDACTED> wrote:

> 
>> sum(N) when is_integer(N), N >= 0 ->
>>    (N * (N+1)) // 2.
>> 
> 
> Should be with a single forward slash.

Sorry.  I use too many programming languages, and notations for integer
division are just pointlessly diverse.  (And since Erlang syntax was
copied from Prolog, it is NOT helpful that it doesn't use Prolog's
integer division operator, see http://en.wiktionary.org/wiki/faux_ami .)

NO, this should NOT be with a forward slash.  It should be

sum(N) when is_integer(N), N >= 0 ->
    (N * (N + 1)) div 2.
%                 ^^^

> Also the OP should know that this function will return a float.

Not if implemented correctly it won't.

Integer division can be
missing in AWK and Perl and APL (⌊x÷y isn't really the same)
  /     in Fortran, and the C family  (/ is used for fixed division
        in PL/I, but 25+1/3 raises FIXEDOVERFLOW)
  \     MUMPS (officially M these days), also MathWorld
  ÷     in Algol 60 and Algol 68 (where it's also OVER)
  div   in the Pascal family and SML
  `div` in Haskell
  //    in Smalltalk, Prolog, Eiffel, and Python 2.2 and later
  quo:  in Smalltalk (which has 2 of the 5 candidates for what
           "integer division" should mean)
  %/%   in the S family (including R).
  (quotient x y) in Scheme
  (floor x y), (truncate x y) (ceiling x y) (round x y) in Lisp

A programming language I used to like a lot used X / Y to mean
X ++ "/" ++ Y.





More information about the erlang-questions mailing list