Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/prelude/PreludeList/Index.hs

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


module Prelude where

infixl 9 !!

(!!) :: [a] -> Int -> a

[]     !! _ = error "PreludeList.!!: on empty list"
(x:_)  !! 0 = x
(_:xs) !! n = 
  if n < 0 then
    error "Prelude.!!: negative index"
  else
    xs `walk` (n-1)
 where
   walk           :: [a] -> Int -> a
   [] `walk` _     = error "Prelude.!!: index too large"
   (x:xs) `walk` 0 = x
   (_:xs) `walk` n = xs `walk` (n-1)


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].