Dabbling around with Haskell

So last night I went to a Haskell workshop.

One of the examples my professor gave was the factorial function:

The first line is the function signature, the second line is the base case, and the third line is the recursive step of the function. One thing I really like about Haskell so far is how clean recursive functions are.

Today I started dabbling a bit more with Haskell and created a recursive function to calculate pi using the Gregory–Leibniz formula:

I had to name it “pie” because “pi” was a reserved keyword. The “fromIntegral n” is there to allow an integer to be part of a fractional calculation. This is necessary because Haskell is über strict with types.

Here it is in action:

Leave a Reply

Your email address will not be published. Required fields are marked *