What's the point of 'const' in the Haskell Prelude?

后端 未结 9 1190
不思量自难忘°
不思量自难忘° 2020-12-12 18:50

Looking through the Haskell Prelude, I see a function const:

const x _ = x

I can\'t seem to find anything relevant regarding t

9条回答
  •  春和景丽
    2020-12-12 19:30

    Say you want to create a list of Nothings equal to the length of a string. As const returns its first argument, no matter the second, you can do:

    listOfNothings :: String -> [Maybe Char]
    listOfNothings = (map . const) Nothing
    

    or, more explicitly:

    listOfNothing st = map (const Nothing) st
    

提交回复
热议问题