Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Haskell

Idiom #77 Complex number

Declare a complex x and initialize it with value (3i - 2). Then multiply it by i.

The complex x is moved on the 2D plane when multiplied by i
let i = ( 0 :+ 1 ) ; x = 3 * i - 2 in x * i
ímport Data.Complex
x = (0 :+ 1) * ((0 - 2) :+ 3)

Declaration and initialization is not required in Haskell.
As prefix negation in Haskell is tricky, I've used (0 - 2) for (-2).
Haskell variables are immutable, therefore "multiply it by two" is both unnecessary and too complicated.
with Ada.Numerics.Complex_Types;
declare
   use Ada.Numerics.Complex_Types;
   X : Complex := (Re => -2.0, Im => 3.0);
begin
   X := X * i;
end;

New implementation...
< >
deleplace