Wednesday, January 11, 2017

HASKELL TRIANGULAR NUMBER SEQUENCE

HASKELL TRIANGULAR NUMBER SEQUENCE

REVISED: Thursday, February 8, 2024


1. INTRODUCTION

A triangle with all three sides of equal length and all angles equal to 60° is an equilateral triangle.

Triangular numbers
t n = (n * (n+1)) `div` 2
are the number of objects needed to form an equilateral triangle.

This tutorial shows a method for using Haskell to compute the Triangular Number Sequence of objects needed to form equilateral triangles:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1035, 1081, 1128, 1176, 1225, 1275, 1326, 1378, 1431...

2. HASKELL PROGRAM

A Haskell program used to compute the Triangular Number Sequence is as follows:

-- C:\Users\Tinnel\Haskell2024\triangularNS.hs

import Data.List
import System.IO

main :: IO ()  -- Has type I/O action.
main = do {  putStrLn ""  -- putStrLn Has type I/O action.
           ; putStrLn "=========================================================="
           ; putStrLn ""
           ; putStrLn "TRIANGULAR NUMBER SEQUENCE"
           ; putStrLn ""
           ; putStrLn "h = 1      2       3         4          5"
           ; putStrLn "      **     ***     ****     *****     ******"
           ; putStrLn "              ***   h****     *****     ******"
           ; putStrLn "                       ****     *****     ******"
           ; putStrLn "                       h+1     *****     ******"
           ; putStrLn "                                              ******"
           ; putStrLn ""
           ; putStrLn "The above rectangles are h high and h + 1 wide  and contain 2 equilateral triangles."
           ; putStrLn ""
           ; putStrLn "t h is the total stars in one equilateral triangle."
           ; putStrLn "t h = h * (h + 1) `div` 2"
           ; putStrLn ""
           ; putStrLn "For example"
           ; putStrLn "t 5 = 5 * (5 + 1) `div` 2"
           ; putStrLn "equals 15 stars in one equilateral triangle 5 stars high."
           ; putStrLn ""
           ; putStrLn "=========================================================="
           ; putStrLn ""
           ; putStrLn "Type a positive integer representing an equilateral triangle with height of h then press Enter."
           ; s <- getLine -- getLine is a function, getLine :: IO String
           ; putStrLn ""
           ; putStrLn "=========================================================="
           ; putStrLn ""
           ; let h = (read s)::Integer
           ; putStrLn "The Triangular Number is:"
           ; putStrLn ""
           ; let z = fromIntegral(t h)
           ; print z
           ; putStrLn ""
           ; putStrLn "=========================================================="
           ; putStrLn ""
          }
--
-- Haskell has different functions for integer and 'fractional' division.
-- Int is not an instance of Fractional so you can't use (/) with Int.
-- t :: Fractional a => a -> a

t :: Integral a => a -> a
t h = h * (h + 1 )`div` 2

3. GHCi RUN

*Main> :l triangularNS
[1 of 1] Compiling Main             ( triangularNS.hs, interpreted )
Ok, modules loaded: Main.
*Main> main

=========================================================================

TRIANGULAR NUMBER SEQUENCE

h = 1      2       3        4          5
      **     ***     ****     *****     ******
              ***   h****     *****     ******
                       ****     *****     ******
                       h+1    *****     ******
                                             ******
The above rectangles are h high and h + 1 wide and contain 2 equilateral triangles.

t h is total stars in one equilateral triangle.
t h = h * (h + 1) `div` 2

For example
t 5 = 5 * (5 + 1) `div` 2
equals 15 stars in one equilateral triangle 5 stars high.

=========================================================================

Type a positive integer representing an equilateral triangle with height of h then press Enter.
5

=========================================================================

The Triangular Number is:
15

=========================================================================

*Main>

4. CONCLUSION

In this tutorial you have seen a basic approach for using Haskell to compute the Triangular Number Sequence.

5. REFERENCES

Bird, R. (2015). Thinking Functionally with Haskell. Cambridge, England: Cambridge University Press.

Davie, A. (1992). Introduction to Functional Programming Systems Using Haskell. Cambridge, England: Cambridge University Press.

Goerzen, J. & O'Sullivan, B. &  Stewart, D. (2008). Real World Haskell. Sebastopol, CA: O'Reilly Media, Inc.

Hutton, G. (2007). Programming in Haskell. New York: Cambridge University Press.

Lipovača, M. (2011). Learn You a Haskell for Great Good!: A Beginner's Guide. San Francisco, CA: No Starch Press, Inc.

Thompson, S. (2011). The Craft of Functional Programming. Edinburgh Gate, Harlow, England: Pearson Education Limited.