Triangular number calculator

Mushy-pea 0 Tallied Votes 99 Views Share

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)
Lardmeister 461 Posting Virtuoso

Interesting, how do you run this program?

Rashakil Fol 978 Super Senior Demiposter Team Colleague

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 978 Super Senior Demiposter Team Colleague

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.