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

后端 未结 9 1191
不思量自难忘°
不思量自难忘° 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:07

    Say you want to rotate a list. This is an idiomatic way to do so in Haskell:

    rotate :: Int -> [a] -> [a] rotate _ [] = [] rotate n xs = zipWith const (drop n (cycle xs)) xs

    This function zips two arrays with the function const, the first being an infinite cyclic array, the second being the array you started with.

    const acts as the bounds check, and uses the original array to terminate the cyclic array.

    See: Rotate a list in Haskell

提交回复
热议问题