From a0527fc18dd66c1b6bef7d7e9f5bd7d1af51394a Mon Sep 17 00:00:00 2001 From: simonmar Date: Tue, 21 Oct 2003 13:27:13 +0000 Subject: [PATCH] [project @ 2003-10-21 13:27:13 by simonmar] Make peekArray0 run in constant stack-space by testing the length of the array first, then calling peekArray (which works backwards from the end so it can be tail-recursive). --- Foreign/Marshal/Array.hs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Foreign/Marshal/Array.hs b/Foreign/Marshal/Array.hs index 002afed..45ea878 100644 --- a/Foreign/Marshal/Array.hs +++ b/Foreign/Marshal/Array.hs @@ -136,13 +136,9 @@ peekArray size ptr | size <= 0 = return [] -- |Convert an array terminated by the given end marker into a Haskell list -- peekArray0 :: (Storable a, Eq a) => a -> Ptr a -> IO [a] -peekArray0 marker ptr = loop 0 - where - loop i = do - val <- peekElemOff ptr i - if val == marker then return [] else do - rest <- loop (i+1) - return (val:rest) +peekArray0 marker ptr = do + size <- lengthArray0 marker ptr + peekArray size ptr -- |Write the list elements consecutive into memory -- -- 1.7.10.4