The need for the simplest Haskell program

Can someone provide me with less than five lines of code that I can save as .hs and run as a haskell program and see how the magic happens? The internet is so complicated sometimes.

+6
source share
10 answers
main = putStrLn "Hello, World!" 

From http://www.haskell.org/haskellwiki/Haskell_in_5_steps

Internet is not so bad!

+12
source

Someone should have mentioned interact , which is simple and practical:

 main = interact reverse 
  $ cat interact.hs |  runhaskell interact.hs 
  esrever tcaretni = niam 
 

and therefore with

 main = interact (unwords . reverse . words) 
  $ cat interact.hs |  runhaskell interact.hs 
  words).  reverse.  (unwords interact = main 
 

or with import

 import Data.List main = interact (intersperse '\n') 
  $ echo "hello" |  runhaskell interact.hs 
  h 
  e 
  l 
  l 
  o 
 

or, now compiling:

 main = interact showCharcount where showCharcount str = show (length str) ++ "\n" 
  $ ghc --make -O2 interact.hs -o charcount 
  $ echo "hello world" |  ./charcount 
  12 
 

In this case, it makes sense to start doing a cue ball benchmarking:

  $ time cat / usr / share / dict / words |  ./charcount 
  2486813 
  real 0m0.096s 
 
+10
source

This answer focused more on the “vision of magic”:

 data Expression = Greeting | Leaving f :: Expression -> String f Greeting = "Hi there!" f Leaving = "Ok, bye!" main = putStrLn (f Greeting) 
+6
source

What about all the Fibonacci numbers? Well, you can just type something like 100 of them for brevity ..;)

 fibs = 1 : scanl (+) 1 fibs main = print $ take 100 fibs 
+5
source

Hamming numbers are numbers that do not have any simple factors greater than 5. Ie they have the form 2 ^ i * 3 ^ j * 5 ^ k. The first 20 of them:

 [1,2,3,4,5,6,8,9,10,12,15,16,18,20,24,25,27,30,32,36] 

500,000th:

 1962938367679548095642112423564462631020433036610484123229980468750 

The program that printed the 500,000th (after a brief moment of calculation):

 merge xxs@ (x:xs) yys@ (y:ys) = case (x`compare`y) of LT -> x:merge xs yys EQ -> x:merge xs ys GT -> y:merge xxs ys hamming = 1 : m 2 `merge` m 3 `merge` m 5 where mk = map (k *) hamming main = print (hamming !! 499999) 

Longer than 5 lines of code you like. Of course, it can be a game of golf, but I would prefer to write it naturally and see how many lines you need to calculate this number in any other language with a reasonable lead time.

+4
source

Printing of each number:

 main = mapM_ print [1..] 
+3
source

You could go have fun. Here greet is a function that takes a name and issues a greeting:

 greet xs = "\nHello, " ++ xs main = do putStrLn $ unlines ["Hi! I'm a Haskell program.", "Who are you?"] fmap greet getLine >>= putStrLn 

main uses unlines to turn a list of lines into a single line, separated by a newline, then print it with putStrLn . getLine returns a user-entered line of text (without a newline), then applies greet to it. Finally, we insert this as an input into another putStrLn .

+3
source

This bit is a little tight and definitely not the easiest, but it uses an endless list [1..] , which you might call magic.

 described name list = putStrLn ("\n" ++ name) >> mapM_ print (zip [1..] list) main = let somenums = [1..100] in do described "Some cubes:" [ x^3 | x <- somenums] described "Some powers:" $ map (2^) somenums described "Some triangle numbers:" $ scanl (+) 0 somenums 

The described function prints a description and then prints a zipped list with their position. I use it on several number sequences. By default, all data by the number is Integer , so he can happily tell you 2^100 . The Int data type is more limited, ( maxBound :: Int == 2147483647 ), but, of course, takes less time and space.

+2
source

From Haskell / String IO Tutorials / Programming, pay attention to a few quick and easy start examples; for example, the equivalent of bash cat < myFile.txt ,

 main = interact id 

where interact applies the function (in this case, the id identification function) to the content from the standard input (here myFile.txt ).

Compile and run it as follows:

 ghc --make short.hs ./short < myFile.txt 
+1
source

All Articles