Haskell is a computer programming language designed by a committee in 1990 to consolidate the best features of the many purely functional programming languages that were created in the late 1980s. Haskell is thus neither a procedural programming language nor an object-oriented one, although it offers monads such as do to support procedural programming and classes with inheritance to support object-oriented programming (there is also a variant of Haskell called O’Haskell which includes more support for object-oriented programming). In general, Haskell programs are most naturally written declaratively.

The standard version of the language is Haskell 98; Haskell 2007 hasn’t been released yet but is expected to be only a minor revision of Haskell 98.

The standard Haskell prelude includes the function gcd, which computes the greatest common divisor of two integers. The following Haskell code is a reimplementation of the gcd function.

-- gcd.hs -- compute the gcd of two integers

mygcd :: Int -> Int -> Int
mygcd m n
| (n < 0)   = mygcd m (abs n)
| (n == 0)  = m
| (m < n)   = mygcd n m
| otherwise = mygcd n (mymod m n)

mydiv :: Int -> Int -> Int
mydiv m n
| (m < 0)   = negate (mydiv (negate m) n)
| (n < 0)   = negate (mydiv m (negate n))
| (m < n)   = 0
| otherwise = 1 + mydiv (m-n) n

mymod :: Int -> Int -> Int
mymod m n = m - n * (mydiv m n)

Title Haskell Haskell 2013-03-22 16:47:13 2013-03-22 16:47:13 PrimeFan (13766) PrimeFan (13766) 6 PrimeFan (13766) Definition msc 68N15