Plan 9 from Bell Labs’s /usr/web/sources/contrib/fernan/nhc98/src/libraries/fps/tests/lazybuildcons.hs

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



-- This encapsulates the pattern of programs that gradually (and lazily)
-- build a bytestring with cons. If cons is too strict in its second
-- argument then we get a stack overflow for large n.

module Main (main) where

import qualified Data.ByteString.Lazy.Char8 as LC
import System.Environment

main :: IO ()
main = do xs <- getArgs
          case xs of
              [x] ->
                  case reads x of
                      [(n, "")] ->
                          LC.putStr $ foo n
                      _ -> error "Bad argument"
              _ -> error "Need exactly 1 argument (number of times to loop)"

foo :: Int -> LC.ByteString
foo 0 = LC.empty
foo (n+1) = 'c' `LC.cons` foo n


Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.