Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Need of the Most Simple Haskell Program

Tags:

haskell

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

like image 556
Emanegux Avatar asked Nov 15 '12 05:11

Emanegux


2 Answers

main = putStrLn "Hello, World!"

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

The internet isn't so bad!

like image 189
slipsec Avatar answered Oct 10 '22 14:10

slipsec


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

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

and thus with

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

or with an 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 which case it makes sense to start doing a bit of poor man's benchmarking:

$ time cat /usr/share/dict/words | ./charcount
2486813
real 0m0.096s
like image 39
applicative Avatar answered Oct 10 '22 14:10

applicative