Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
989 views
in Technique[技术] by (71.8m points)

haskell - What does eta reduce mean in the context of HLint

I'm looking at the tutorial http://haskell.org/haskellwiki/How_to_write_a_Haskell_program

import System.Environment

main :: IO ()
main = getArgs >>= print . haqify . head

haqify s = "Haq! " ++ s

When running this program under HLint it gives the following error;

./Haq.hs:11:1: Warning: Eta reduce
Found:
  haqify s = "Haq! " ++ s
Why not:
  haqify = ("Haq! " ++ )

Can someone shed some light on what exactly "Eta Reduce" means in this context?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Eta reduction is turning x -> f x into f as long as f doesn't have a free occurence of x.

To check that they're the same, apply them to some value y:

(x -> f x) y === f' y -- (where f' is obtained from f by substituting all x's by y)
              === f y  -- since f has no free occurrences of x

Your definition of haqify is seen as s -> "Haq! " ++ s, which is syntactic sugar for s -> (++) "Haq! " s. That, in turn can be eta-reduced to (++) "Haq! ", or equivalently, using section notation for operators, ("Haq! " ++).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...