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:  = vs <- (Kyle Murphy)
   2. Re:  Re: ghci access to .hs functions (MAN)


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

Message: 1
Date: Thu, 12 Aug 2010 10:39:42 -0400
From: Kyle Murphy <[email protected]>
Subject: Re: [Haskell-beginners] = vs <-
To: prad <[email protected]>
Cc: haskellbeginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

To really understand the difference between = and <- you need to
understand the syntactic sugar that the do notation provides. Given
the following:

myExample :: Int -> IO Int
myExample num = return num

You could write the following:

main = do
  theNum <- myExample 42
  putStrLn (show theNum)

This is exactly the same as writing the following:

main = (myExample 42) >>= (\theNum -> putStrLn (show theNum))

The type of >>= is:

m a -> (a -> m b) -> m b

Remember that in most cases, once something is "inside" of a monad (IO
in this case), it's very difficult, impossible, to get it out again.
What >>= provides, is a way of taking a value out of a monad, using
some function on it that returns a new value inside that same monad,
and then returning that. For example, once the types get bound, the
>>= in the above example has type:

IO Int -> (Int -> IO ()) -> IO ()

The m has been bound to IO, a is bound to Int, and b has been bound to
(). This is because the type of putStrLn is String -> IO (). The do
syntax just adds a convenient abstraction so that you don't have to
construct complex chains of >>= and >> operations by hand. In our
limited example this is fairly trivial, but consider something like:

main = do
  something <- foo
  blah <- bar something
  moreblah <- baz something blah
  putStrLn moreblah
  putStrLn "really long chain now"

which would be translated to:

main = foo >>= (\something -> bar something >>= (\blah -> baz
something blah >>= (\moreblah -> putStrLn moreblah >> (putStrLn
"really long chain now"))))

Conceptually you can think of (<-) as having type:

a <- IO a

although since (<-) isn't really an op (it's syntactic sugar that's
only valid inside the do notation, it doesn't exist after you remove
the sugar) it of course doesn't really have a type. This also why you
can't just use (=) inside of do notation, and instead have to use let,
you need to escape out of the sugar before you can use it. For an
example of why this is, consider the following:

main = do
  something = foo
  putStrLn something

which would be translated into:

main = something = foo >>= (putStrLn  something)

which of course doesn't make any sense at all.

-R. Kyle Murphy
--
Curiosity was framed, Ignorance killed the cat.



On Wed, Aug 11, 2010 at 16:12, prad <[email protected]> wrote:
> i'm using them right i think because it works, but my understanding is
> fuzzy.
>
> it seems <- is used when you do things like load a file or get
> arguments from outside or if you are return something from a function
>
> these seem to take the form of something other than an internal type
> like Int or String and have to be 'massaged' into use with things like
> show or fromSql etc
>
> the = is used with let and seems to work for any assignments that are
> internal.
>
> are these assessments correct?
>
> --
> In friendship,
> prad
>
>                                      ... with you on your 
> journey
> Towards Freedom
> http://www.towardsfreedom.com (website)
> Information, Inspiration, Imagination - truly a site for soaring I's
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>


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

Message: 2
Date: Thu, 12 Aug 2010 12:00:44 -0300
From: MAN <[email protected]>
Subject: Re: [Haskell-beginners] Re: ghci access to .hs functions
To: prad <[email protected]>
Cc: [email protected]
Message-ID: <1281625244.2386.25.ca...@dy-book>
Content-Type: text/plain; charset="UTF-8"

Hey prad, I was checking out your code and noticed a couple of things
you could be interested to know.

Your main will allways be 'IO ()' , but that doesn't mean you must
sparkle 'return ()' all over the place :P Check this out:

main = do
    args <- getArgs
    let act = head args
    conn <- connectPostgreSQL "host=localhost dbname=lohv user=pradmin"
    case act of 
         "add"  -> do
             kV1 <- dbDef conn
             upDbs conn (fromSql kV1)
         "upd"  -> upDbs conn (last args)
         "all"  -> gtKys conn
         _      -> putStrLn "add, upd num, all only!!" 
    commit conn
    disconnect conn
    putStrLn "All Done!"

I took the liberty of modifying your gtKys function:

-- gtKys: gets all key values in database [NOTICE mapM_ for mapM]
gtKys :: (IConnection conn) => conn -> IO ()
gtKys conn = do
    r <- quickQuery conn "SELECT key from main" []
    let kL = concat $ map (map fromSql) r
    mapM_ (mkPag conn) kL

Only if 'upDbs' return some value inside IO you'd replace their calls
for " upDbs conn some >> return () ". Although I assume those statements
are simple 'IO ()', just like 'putStrLn string'


El jue, 12-08-2010 a las 01:42 -0700, prad escribió:
> On Thu, 12 Aug 2010 09:01:42 +0100
> Brent Yorgey <[email protected]> wrote:
> 
> > Can you show
> > us the exact contents of your .hs file?
> certainly! but now i take it all back! :(
> it's working fine without the return().
> 
> it compiles main when i :l or when i ghci the file from the command
> line.
> 
> so now i think i'm delusional. :D :D
> 
> anyway, here's some of the code below and i'll ask another question. in
> the function:
> 
> -- edFil: edits a file with vim
> -- edFil :: String -> IO GHC.IO.Exception.ExitCode (not in scope error) 
> edFil kV = rawSystem "vim" ["+source ~/.vim/ftplugin/html/HTML.vim",kV]
> 
> i got the type from ghci, but if i actually put it in i get error:
> Not in scope: type constructor or class `GHC.IO.Exception.ExitCode'
> 
> if i don't do the type definition that ghci gives me, everything
> compiles fine.
> 
> so i don't understand the type definition, much less what's happening
> here.
> 
> ======
> import System (getArgs)
> import System.Cmd (rawSystem)
> import Data.List(elemIndices)
> import Database.HDBC
> import Database.HDBC.PostgreSQL (connectPostgreSQL)
> 
> main = do
>     args <- getArgs
>     let act = head args
>     conn <- connectPostgreSQL "host=localhost dbname=lohv user=pradmin"
>     case act of 
>          "add"  -> do
>              kV1 <- dbDef conn
>              upDbs conn (fromSql kV1)
>              return ()
>          "upd"  -> do
>              upDbs conn (last args)
>              return ()
>          "all"  -> do
>              gtKys conn
>              return ()
>          _      -> putStrLn "add, upd num, all only!!" 
>     commit conn
>     disconnect conn
>     putStrLn "All Done!"
>     return  ()
> 
> 
> -- bkS2L: break a string into a list of strings
> --  dC char delimiter; oS original string 
> bkS2L :: (Char -> Bool) -> String -> [String]
> bkS2L dC [] = []
> bkS2L dC oS = let (h,t) = break dC oS
>                   in  h : case t of
>                                []   -> []
>                                _:t  -> bkS2L dC t
> 
> -- dbDef: adds a default entry to db
> dbDef :: (IConnection conn) => conn -> IO SqlValue
> dbDef conn = do
>     run conn "INSERT INTO main DEFAULT VALUES" []
>     ((r:z):zs) <- quickQuery conn "SELECT last_value from
> main_key_seq" [] return r
> 
> -- edFil: edits a file with vim
>     -- edFil :: String -> IO GHC.IO.Exception.ExitCode (not in scope
> error) 
> edFil kV = rawSystem "vim" ["+source ~/.vim/ftplugin/html/HTML.vim",kV]
> 
> -- gtInx: gets indices for each element of substring in string
> gtInx :: (Eq a) => [a] -> [a] -> [(a,[Int])]
> gtInx hL nL = map (\x -> (x,elemIndices x hL)) nL
> 
> -- gtKys: gets all key values in database
> gtKys :: (IConnection conn) => conn -> IO [()]
> gtKys conn = do
>     r <- quickQuery conn "SELECT key from main" []
>     let kL = concat $ map (map fromSql) r
>     mapM (mkPag conn) kL
> 
> ...
> 
> =======
> 
> there are more functions, but it is all working fine.
> 
> 
> 




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

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


End of Beginners Digest, Vol 26, Issue 27
*****************************************

Reply via email to