Haskell-one-line

Haskell have many beautiful features,such as monad(combination),so we can write very elegant code(compact code).

Reference Here verbose:


module Main where

import System.IO

main = do
  theInput <- readFile "input.txt"
  putStrLn (countLines theInput)

countLines :: String -> String
countLines str = (show (length (lines str)))

elegant:

module Main where

import System.IO

main = readFile "input.txt" >>= print.length.lines
	      

Function Application

verbose:

not (odd 4)
not $ odd 4

elegant: The . Function It composes functions in a readable manner


f(g(h(k(x)))) is ugly
(f.g.h.k)(x) is pretty
 (not.odd) 4
 (length.head.words) "University of Virginia"

上面写的elegant风格都可以叫做point-free风格,它使用Monad的结合律,

比如下面转换一般代码到point-free风格的方法:

f x = 5 + 8/x
f x = (+) 5 (8/x)
f x = (+) 5 ((/) 8 x)
f x = ((+) 5) ((/) 8 x)
f x = ((+) 5) (((/) 8) x)
f x = ((+) 5).(((/) 8)) x

f= ((+) 5).((/) 8)
f= (5+).(8/)

另一个常用的转换写法:

add a b = a+ b  ---> add =(+)
f s =length (head (words s)) ---> f= length.head.words

Add later

令狐冲
令狐冲
Engineer of offshore wind turbine technique research

My research interests include distributed energy, wind turbine power generation technique , Computational fluid dynamic and programmable matter.

Related

本站访问量: