[Haskell-cafe] how to print out intermediate results in a recursive function?

yi huang yi.codeplayer at gmail.com
Sat Feb 4 05:32:35 CET 2012


You can use trace from Debug.Trace, change the code like this:

mergesort l = case trace l l of
    [] -> ...
    [x] -> ...
    (x:xs) -> ...

On Sun, Feb 5, 2012 at 2:23 AM, Qi Qi <qiqi789 at gmail.com> wrote:

> Hello,
>
> I have a question;how can I print out the intermediate number lists in a
> mergesort recursive function like the following one.
>
> merge [] ys = ys
> merge xs [] = xs
> merge (x:xs) (y:ys) = if x <= y
>                      then x : merge xs (y:ys)
>                      else y : merge (x:xs) ys
>
> mergesort [] = []
> mergesort [x] = [x]
> mergesort xs = let (as, bs) = splitAt (length xs `quot` 2) xs
>               in merge (mergesort as) (mergesort bs)
>
> main = do
>       print $ mergesort [5,4,3,2,1]
>
>
> In the main function, it only prints out the final number list. But I'd
> like to print out the number lists in every recursive level. How can I
> do that? Thanks.
>
>
> --
> Qi Qi
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
http://www.yi-programmer.com/blog/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120204/5e784268/attachment.htm>


More information about the Haskell-Cafe mailing list