Newsgroups: alt.comp.lang.haskell
From:
"levi" <leaveye.... @gmail.com>
Date: 20 Jan 2007 18:43:47 -0800
Local: Sat, Jan 20 2007 9:43 pm
Subject: A question about return value of a recursive function
Hi all. I am a beginner in haskell, reading YetAnotherHaskellTutorial and using GHC 6.6. I am tring to simulate a video encoder with haskell, during this, i found something i could not understand. The code will follow my description. The key function is "doEncode bss cnt refs". The program output shows that no matter what argument i gave, it always returns the result of the outest call. I can not find convincing explanation for this. The code is following. -------- test.lhs ---- start
> module Main
> where
> import IO > import List > main = do > putStrLn $ "================" > aSeq <- getImageSeq > putStrLn $ show aSeq > bss <- doEncode [] 0 [] > putStrLn $ show bss > putStrLn $ "================" > aList <- downList 3 > putStrLn $ show aList > putStrLn $ "================" > type Byte = Int > type Image = [ Byte ] > type Picture = ( Image ) > type BitStream = [ Int ] > getImageSeq :: IO [Image] > getImageSeq = do > return [ [9], [10], [8] ] > getOnePic :: Int -> IO (Maybe Picture) > getOnePic cnt = do > s' <- getImageSeq > let s = drop cnt s' > let pic = if length s > 0 > then Just ( head s ) > else Nothing > return pic > doEncode :: [BitStream] -> Int -> [Picture] -> IO [BitStream] > doEncode bss cnt refs = do > pic <- getOnePic cnt > case pic of > Just p -> do > let (recpic, bs) = encode refs p > let newbss = bss ++ [bs] > let newrefs = recpic : refs > let newm = cnt + 1 > --putStrLn $ "<debug> - doEncode " ++ (show newbss) ++ " " > -- ++ (show newm) ++ " " > -- ++ (show newrefs) > result <- doEncode newbss newm newrefs > putStrLn $ "<debug> + doEncode " ++ (show newbss) ++ " " > ++ (show newm) ++ " " > ++ (show newrefs) > putStrLn $ "<debug> \\ returns : " ++ (show result) > return result > Nothing -> do return bss > encode :: [Picture] -> Picture -> (Picture,BitStream) > encode [] ( img ) = -- if there is not any ref > let bs = 1 : img > recpic = ( img ) > in ( recpic, bs ) > encode refpics pic@(img) = -- if there is some ref > let bs = 0 : map ( uncurry (-) ) ( ( zip img . head ) refpics ) > recpic = ( img ) > in ( recpic, bs ) > downList :: Int -> IO [Int] > downList x = do > if x >= 0 > then do > let x' = x - 1 > putStrLn $ "<dbg> > downList " ++ (show x') > result' <- downList x' > putStrLn $ "<dbg> < downList " ++ (show x') ++ " returns " ++ (show result') > let result = x : result' > return result > else return []
-------- test.lhs ---- end
You must
Sign in before you can post messages.
You do not have the permission required to post.