Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Re: subset (Maciej Piechotka)
   2.  Tracing a program with putStrLn (legajid)
   3. Re:  Tracing a program with putStrLn (Michael Snoyman)
   4. Re:  Tracing a program with putStrLn (Daniel Fischer)
   5. Re:  Tracing a program with putStrLn (legajid)
   6.  Using Debug.Trace (legajid)
   7.  Performance of Idiomatic lazy Haskell (Markus B?hm)


----------------------------------------------------------------------

Message: 1
Date: Fri, 29 Jan 2010 16:13:01 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: subset
To: [email protected]
Message-ID: <1264781580.5629.4.ca...@picard>
Content-Type: text/plain; charset="UTF-8"

On Fri, 2010-01-29 at 07:07 +0000, Luca Ciciriello wrote:
> Hi All.
>  
> Does exist a simple way to check if a lists xs is a subset of another
> list ys?
>  
> Thanks in advance
>  
> Luca.
> 

xs `subsetOf` ys = null $ filter (not . (`elem` ys)) xs

I.e. We take all elements which are not in potential superset and if
there is none then the list is subset.

Regards




------------------------------

Message: 2
Date: Sat, 30 Jan 2010 23:00:23 +0100
From: legajid <[email protected]>
Subject: [Haskell-beginners] Tracing a program with putStrLn
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

i wrote a program that doesn't behave as i wanted.
So i tried to putStrLn my data structures to see what happened. Since i 
did this, all my functions changed type, so i had to modify them (main 
and calcul : use <- instead of let , add returns) and so on for each 
function called. Debugging this way causes  more trouble than it should 
save.

What is the way to trace data thru function recursive calls without 
changing program structures? Is putStrLn a good idea ?
I'm not used to ghci debug and i find it hard to manage (:break, :trace, 
:cont ...).
Is the 'debugged' version as efficient as the first one, due to impure 
functional code? What if i want to remove 'debug code' ?  Should i  
modify back my functions ?

Here are two very simplified versions of my program, the first one 
without 'trace', the second modified to include putStrLn as wanted.


Thanks in advance,
Didier


First version
-------------

main=do
    let valeurs=[0,1,3,0]
    let (valeurs_new)=calcul valeurs
    afficher_resultat valeurs
    afficher_resultat valeurs_new

calcul :: [Int] -> [Int]
calcul xv 
    | nblibr == 0    = map (*2) xv
    | otherwise    = map (+1) xv
                    
    where
            libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
            nblibr=length libres

afficher_resultat xv = do
    putStrLn (show xv)


Second version
----------------

main=do
    let valeurs=[0,1,3,0]

    valeurs_new <- calcul valeurs

    afficher_resultat valeurs
    afficher_resultat valeurs_new

calcul :: [Int] -> IO [Int]
calcul xv  
    | nblibr == 0    = do
                putStrLn "ok"
                return (map (*2) xv)
    | otherwise    =     return (map (+1) xv)
                   
    where
            libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
            nblibr=length libres

afficher_resultat xv = do
    putStrLn (show xv)



------------------------------

Message: 3
Date: Sun, 31 Jan 2010 00:01:57 +0200
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] Tracing a program with putStrLn
To: legajid <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

I think you want the Debug.Trace module. The documentation is available at
http://haskell.org/ghc/docs/latest/html/libraries/base/Debug-Trace.html.

Let me know if you have any questions,
Michael

On Sun, Jan 31, 2010 at 12:00 AM, legajid <[email protected]> wrote:

> Hi,
>
> i wrote a program that doesn't behave as i wanted.
> So i tried to putStrLn my data structures to see what happened. Since i did
> this, all my functions changed type, so i had to modify them (main and
> calcul : use <- instead of let , add returns) and so on for each function
> called. Debugging this way causes  more trouble than it should save.
>
> What is the way to trace data thru function recursive calls without
> changing program structures? Is putStrLn a good idea ?
> I'm not used to ghci debug and i find it hard to manage (:break, :trace,
> :cont ...).
> Is the 'debugged' version as efficient as the first one, due to impure
> functional code? What if i want to remove 'debug code' ?  Should i  modify
> back my functions ?
>
> Here are two very simplified versions of my program, the first one without
> 'trace', the second modified to include putStrLn as wanted.
>
>
> Thanks in advance,
> Didier
>
>
> First version
> -------------
>
> main=do
>   let valeurs=[0,1,3,0]
>   let (valeurs_new)=calcul valeurs
>   afficher_resultat valeurs
>   afficher_resultat valeurs_new
>
> calcul :: [Int] -> [Int]
> calcul xv   | nblibr == 0    = map (*2) xv
>   | otherwise    = map (+1) xv
>                     where
>           libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
>           nblibr=length libres
>
> afficher_resultat xv = do
>   putStrLn (show xv)
>
>
> Second version
> ----------------
>
> main=do
>   let valeurs=[0,1,3,0]
>
>   valeurs_new <- calcul valeurs
>
>   afficher_resultat valeurs
>   afficher_resultat valeurs_new
>
> calcul :: [Int] -> IO [Int]
> calcul xv     | nblibr == 0    = do
>               putStrLn "ok"
>               return (map (*2) xv)
>   | otherwise    =     return (map (+1) xv)
>                     where
>           libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
>           nblibr=length libres
>
> afficher_resultat xv = do
>   putStrLn (show xv)
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100130/a4fcda1f/attachment-0001.html

------------------------------

Message: 4
Date: Sat, 30 Jan 2010 23:18:17 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Tracing a program with putStrLn
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Samstag 30 Januar 2010 23:01:57 schrieb Michael Snoyman:
> I think you want the Debug.Trace module. The documentation is available
> at
> http://haskell.org/ghc/docs/latest/html/libraries/base/Debug-Trace.html.

Yes. Definitely.

As a further tip:

import Debug.Trace

infixl 0 `debug`

debug :: a -> String -> a
debug = flip trace

(of course you can call it something else)

With 'debug', it's more convenient to add and comment out tracing output:

fonction x = trace ("fonction " ++ show x) (travail x)

~> fonction x =  {- trace ("fonction " ++ show x) -} (travail x)

vs.

fonction x = travail x `debug` "fonction " ++ show x

~> fonction x = travail x -- `debug` "fonction " ++ show x


>
> Let me know if you have any questions,
> Michael
>
> On Sun, Jan 31, 2010 at 12:00 AM, legajid <[email protected]> wrote:
> > Hi,
> >
> > i wrote a program that doesn't behave as i wanted.
> > So i tried to putStrLn my data structures to see what happened. Since
> > i did this, all my functions changed type, so i had to modify them
> > (main and calcul : use <- instead of let , add returns) and so on for
> > each function called. Debugging this way causes  more trouble than it
> > should save.
> >
> > What is the way to trace data thru function recursive calls without
> > changing program structures? Is putStrLn a good idea ?
> > I'm not used to ghci debug and i find it hard to manage (:break,
> > :trace,
> >
> > :cont ...).
> >
> > Is the 'debugged' version as efficient as the first one, due to impure
> > functional code? What if i want to remove 'debug code' ?  Should i 
> > modify back my functions ?
> >
> > Here are two very simplified versions of my program, the first one
> > without 'trace', the second modified to include putStrLn as wanted.
> >
> >
> > Thanks in advance,
> > Didier
> >
> >
> > First version
> > -------------
> >
> > main=do
> >   let valeurs=[0,1,3,0]
> >   let (valeurs_new)=calcul valeurs
> >   afficher_resultat valeurs
> >   afficher_resultat valeurs_new
> >
> > calcul :: [Int] -> [Int]
> > calcul xv   | nblibr == 0    = map (*2) xv
> >
> >   | otherwise    = map (+1) xv
> >
> >                     where
> >           libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
> >           nblibr=length libres
> >
> > afficher_resultat xv = do
> >   putStrLn (show xv)
> >
> >
> > Second version
> > ----------------
> >
> > main=do
> >   let valeurs=[0,1,3,0]
> >
> >   valeurs_new <- calcul valeurs
> >
> >   afficher_resultat valeurs
> >   afficher_resultat valeurs_new
> >
> > calcul :: [Int] -> IO [Int]
> > calcul xv     | nblibr == 0    = do
> >               putStrLn "ok"
> >               return (map (*2) xv)
> >
> >   | otherwise    =     return (map (+1) xv)
> >
> >                     where
> >           libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
> >           nblibr=length libres
> >
> > afficher_resultat xv = do
> >   putStrLn (show xv)
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners



------------------------------

Message: 5
Date: Sun, 31 Jan 2010 00:12:35 +0100
From: legajid <[email protected]>
Subject: Re: [Haskell-beginners] Tracing a program with putStrLn
To: Michael Snoyman <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

OK, that's a great help.


Michael Snoyman a écrit :
> I think you want the Debug.Trace module. The documentation is 
> available at 
> http://haskell.org/ghc/docs/latest/html/libraries/base/Debug-Trace.html.
>
> Let me know if you have any questions,
> Michael
>
> On Sun, Jan 31, 2010 at 12:00 AM, legajid <[email protected] 
> <mailto:[email protected]>> wrote:
>
>     Hi,
>
>     i wrote a program that doesn't behave as i wanted.
>     So i tried to putStrLn my data structures to see what happened.
>     Since i did this, all my functions changed type, so i had to
>     modify them (main and calcul : use <- instead of let , add
>     returns) and so on for each function called. Debugging this way
>     causes  more trouble than it should save.
>
>     What is the way to trace data thru function recursive calls
>     without changing program structures? Is putStrLn a good idea ?
>     I'm not used to ghci debug and i find it hard to manage (:break,
>     :trace, :cont ...).
>     Is the 'debugged' version as efficient as the first one, due to
>     impure functional code? What if i want to remove 'debug code' ?
>      Should i  modify back my functions ?
>
>     Here are two very simplified versions of my program, the first one
>     without 'trace', the second modified to include putStrLn as wanted.
>
>
>     Thanks in advance,
>     Didier
>
>
>     First version
>     -------------
>
>     main=do
>       let valeurs=[0,1,3,0]
>       let (valeurs_new)=calcul valeurs
>       afficher_resultat valeurs
>       afficher_resultat valeurs_new
>
>     calcul :: [Int] -> [Int]
>     calcul xv   | nblibr == 0    = map (*2) xv
>       | otherwise    = map (+1) xv
>                         where
>               libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
>               nblibr=length libres
>
>     afficher_resultat xv = do
>       putStrLn (show xv)
>
>
>     Second version
>     ----------------
>
>     main=do
>       let valeurs=[0,1,3,0]
>
>       valeurs_new <- calcul valeurs
>
>       afficher_resultat valeurs
>       afficher_resultat valeurs_new
>
>     calcul :: [Int] -> IO [Int]
>     calcul xv     | nblibr == 0    = do
>                   putStrLn "ok"
>                   return (map (*2) xv)
>       | otherwise    =     return (map (+1) xv)
>                         where
>               libres = [ x | x <- [1..length xv] ,  xv !! (x-1) == 0]
>               nblibr=length libres
>
>     afficher_resultat xv = do
>       putStrLn (show xv)
>
>     _______________________________________________
>     Beginners mailing list
>     [email protected] <mailto:[email protected]>
>     http://www.haskell.org/mailman/listinfo/beginners
>
>


------------------------------

Message: 6
Date: Sun, 31 Jan 2010 10:25:58 +0100
From: legajid <[email protected]>
Subject: [Haskell-beginners] Using Debug.Trace
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi,

Starting with trace, i have trouble  with my calcul'' function while 
calcul' is ok.
When afftrace in calcul'' is commented, the program compiles.
When uncommented (            afftrace ("    calcul'' vide") ), i get 
the following messages :

-------------------------------------------------------------------------------------------
 *Main> :r
[1 of 1] Compiling Main             ( sud3c.hs, interpreted )

sud3c.hs:62:3:
    Couldn't match expected type `[Char]'
           against inferred type `(Plateau, Char)'
    In a stmt of a 'do' expression: afftrace ("    calcul'' vide")
    In the expression:
        do afftrace ("    calcul'' vide")
           (pxv, False)
    In the definition of `calcul''':
        calcul'' pxv _ []
                   = do afftrace ("    calcul'' vide")
                        (pxv, False)
Failed, modules loaded: none.
-------------------------------------------------------------------------------------------

I don't understand why, in calcul', it's ok and why, in calcul'', it's 
problematic. Because return value of calcul'' is a tuple ?

When my program is ok, should i remove all trace instructions (and 
associated do commands too) or just set  my modetrace value to False ?
 

Thanks for helping,
Didier

Below my code :


calcul' :: Plateau -> [Cellule] -> Plateau
calcul' pxv  [] = do
            afftrace ("  Calcul' vide")
            pxv
calcul' pxv  (c:cs)=    do
            afftrace ("  Calcul' ok1="++show ok1++" c:cs= "++show (c:cs))
            afftrace ("  "++show xv1)
               
            if ok1  then do
                    afftrace ("  Calcul' suite")
                    calcul' xv1 cs   
                else do
                    afftrace ("  Calcul' pas de valeur")
                    pxv
    where
        vallib=[1..length pxv]++[5..7]
        nbvlib=length vallib
        (xv1,ok1)=calcul'' pxv c vallib



calcul'' :: Plateau -> Cellule -> [Valeur] -> (Plateau, Bool)
calcul'' pxv  _ [] = do
            --afftrace ("    calcul'' vide")
            (pxv, False)

calcul'' pxv  c (li : lis) = do
                --afftrace ("    "++show pxv)
                --afftrace ("    "++show c ++ "   "++show(li:lis))
                v2
                where
                  v2= if (elem li pxv || li==4)
                    then    
                        calcul'' pxv  c lis
                    else do
                        --afftrace ("      calcul'''")
                        (calcul''' pxv c li, True)

calcul''' :: Plateau -> Cellule -> Valeur -> Plateau
calcul''' pxv c li =
    take (c-1) pxv ++ [li] ++ drop c pxv

afftrace x= if modetrace then trace x " "
             else " "


------------------------------

Message: 7
Date: Sun, 31 Jan 2010 11:14:36 +0100
From: Markus B?hm <[email protected]>
Subject: [Haskell-beginners] Performance of Idiomatic lazy Haskell
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

As a beginner fascinated about Haskell's lazy, idiomatic style I tried
a small example:
Calculate a numerical approximation of PI based on the Leibniz formula.

Variant 1: My lazy, idiomatic approach (as I understand it so far).
It takes on my machine with eps = 0.00000001 approx. 25 sec.

import Data.List

main :: IO ()
main = do putStrLn "EPS: "
          eps <- readLn :: IO Double
          let pi14 = calcpi14 eps
          putStrLn $ "PI mit EPS "++(show eps)++" = "++ show(4*pi14)

calcpi14:: Double->Double
calcpi14 eps = foldl' (+) 0 $ takeWhile (\x -> 4 * abs x > eps) ev
     where e = map (\x->1.0/x) [1.0,3.0..]
           ev = zipWith (*) e $ cycle [1.0,-1.0]


Variant 2: My "C-style" approach achieving state via additonal
function parameters.
It takes on my machine with eps = 0.00000001 approx. 5 sec.

import Data.List

main :: IO ()
main = do putStrLn "EPS: "
          eps <- readLn :: IO Double
          let pi14 = pisum 1 3 False eps
          putStrLn $ "PI mit EPS "++(show eps)++" = "++ show(4*pi14)

pisum s i _     eps | 4/i < eps = s
pisum s i True  eps =
    let h = s + 1/i
    in h `seq` pisum h (i+2) False eps
pisum s i False eps =
    let h = s - 1/i
    in h `seq` pisum h (i+2) True eps

I'd like to do idiomatic, lazy Haskell w/o such a performance penalty?
How can I learn/achive that?


-- Markus


------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 19, Issue 32
*****************************************

Reply via email to