954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Triangular number calculator

0
By Mushy-pea on Feb 15th, 2007 10:04 pm

Well, I asked Dani and she said I could create a Haskell catagory in code snippits just by submitting a Haskell program :p . What self respecting code repository would be without one? So here goes, this program calculates the nth triangular number, given n by the user.

Steven.

module Main
      where

result x = foldr (+) 0 [1..read x]

main = do
putStrLn("Your input: ")
user_data <- getLine
putStrLn("My output: ")
print(result user_data)

Interesting, how do you run this program?

Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
 

It's not good form to put the body of a 'do' block (or any other artifact) on the first row. It's probably illegal, and although GHC accepts the syntax, it won't let you put any more top-level definitions in the file.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

Oh, and to run this program, type 'ghc --make foo.hs' (where foo.hs is your actual filename) to compile and then run the program foo. You'll need to get GHC though.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You