Ori
Ori

Reputation: 154

Haskell desugar syntax

Shuklan's Haskell Lecture wanted the following code desugared:

main = do
  putStrLn "Enter name:"
  name <- getLine
  putStrLn ("Hi " ++ name)

I came up with:

main = putStrLn "Enter name:" >> getLine >>= \str -> putStrLn ("Hi " ++ str)

He revealed:

main = putStrLn "Enter name:" >> getLine >>= putStrLn . ("Hi " ++)

Never seen that syntax before, how does it work?

Upvotes: 3

Views: 262

Answers (1)

that other guy
that other guy

Reputation: 123660

The snippets are identical, the latter just uses point free style (also punningly referred to as "pointless style").

The central point is that ("Hi " ++) is a partially applied (++) that prepends "Hi " to the input.

This function is composed (using .) with putStrLn to get a function that prepends "Hi " to the input and then prints it.

This is exactly what your more explicit lambda does.

Upvotes: 8

Related Questions