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: mayBe stuck (prad)
   2.  global variables (prad)
   3.  = vs <- (prad)
   4.  ghci access to .hs functions (prad)
   5.  Re: global variables (Ertugrul Soeylemez)
   6.  Re: = vs <- (Ertugrul Soeylemez)
   7. Re:  global variables (MAN)
   8. Re:  = vs <- (Benjamin Edwards)
   9. Re:  global variables (Gaius Hammond)


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

Message: 1
Date: Wed, 11 Aug 2010 12:47:57 -0700
From: prad <[email protected]>
Subject: [Haskell-beginners] Re: mayBe stuck
To: [email protected]
Message-ID: <20100811124757.06e44...@gom>
Content-Type: text/plain; charset=US-ASCII

On Sat, 7 Aug 2010 09:26:10 +0100
Brent Yorgey <[email protected]> wrote:

> > also, is it better to use pattern matching (x:xs) than head and
> > tail? could that be part of the point here too?  
> 
> Yes, that's exactly right.
>
ok thx!

inspired by this i'm minimizing using head and tail everywhere i can.
i like pattern matching even when the expression becomes complex
because you see what you get more easily, imho.

there are some rare situations where i've used head, but only because it
would be very awkward to try to pattern match.

-- 
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




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

Message: 2
Date: Wed, 11 Aug 2010 12:54:18 -0700
From: prad <[email protected]>
Subject: [Haskell-beginners] global variables
To: haskellbeginners <[email protected]>
Message-ID: <20100811125418.7a1cd...@gom>
Content-Type: text/plain; charset=US-ASCII

i'm curious about the best way to deal with conn to make db connections.

i define it in main like this, then when i want to use it in a function
i have to pass it as a parameter:

main = do
    conn <- connectPostgreSQL ...
    fn conn something


fn conn x = ...


now conn is a global variable i guess because it is defined in main,
but perhaps main isn't considered a global namespace? i recall in
python a global variable could be accessed by functions, but in php
they can't unless you use "global" within the function.

so is this the correct way to handle such things? right now it seems
to be the only way in haskell.

-- 
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


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

Message: 3
Date: Wed, 11 Aug 2010 13:12:27 -0700
From: prad <[email protected]>
Subject: [Haskell-beginners] = vs <-
To: haskellbeginners <[email protected]>
Message-ID: <20100811131227.2d0c8...@gom>
Content-Type: text/plain; charset=US-ASCII

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


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

Message: 4
Date: Wed, 11 Aug 2010 13:17:09 -0700
From: prad <[email protected]>
Subject: [Haskell-beginners] ghci access to .hs functions
To: haskellbeginners <[email protected]>
Message-ID: <20100811131709.2d610...@gom>
Content-Type: text/plain; charset=US-ASCII

in an effort to figure out type declarations i thought i'd let ghci do
the work.

so i wrote a program:

import ...
main = do
    ...
func1
func2

now when i load this into ghci i can't to :t func1 etc and get a not in
scope error

however, if i put a return () onto the end of main:

import ...
main = do
    ...
    return ()
func1
func2

the functions are in scope of ghci and i can find out the types.

so what is happening here? i understand that return is different in
haskell than in other languages, but i don't understand just what it is
doing. :(

-- 
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


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

Message: 5
Date: Wed, 11 Aug 2010 22:25:45 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: global variables
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

prad <[email protected]> wrote:

> i'm curious about the best way to deal with conn to make db
> connections.
>
> i define it in main like this, then when i want to use it in a
> function i have to pass it as a parameter:
>
> main = do
>     conn <- connectPostgreSQL ...
>     fn conn something
>
>
> fn conn x = ...
>
>
> now conn is a global variable i guess because it is defined in main,
> but perhaps main isn't considered a global namespace? i recall in
> python a global variable could be accessed by functions, but in php
> they can't unless you use "global" within the function.
>
> so is this the correct way to handle such things? right now it seems
> to be the only way in haskell.

I'd say the closest to a global variable in Haskell is a top level
definition.  However, that definition is fixed by the programmer and
cannot be changed at run time, of course.  What you want is not a global
variable, but a convenient way to pass environment values to functions
and computations.  My way of doing this is the following (snippet):

  import Control.Applicative
  import MonadLib

  type DatabaseT = ReaderT Connection

  myApplication :: DatabaseT IO ()
  myApplication = do
    dbConn <- ask
    inBase $ runRaw dbConn "SELECT now();"

  runDatabaseT :: BaseM m IO => DatabaseT m a -> m a
  runDatabaseT c = do
    conn <- inBase $ connectPostgreSQL ""
    runReaderT (Config conn) c

  main :: IO ()
  main = runDatabaseT myApplication

This is really convenient and useful for modularizing your code.  You
can easily write a monad transformer for every part of your program.
However, using this approach you will generally stick together multiple
state and reader monads together.  So it may be more convenient to use a
newtype instead of a type alias.  For example a game could look like
this (using newtypes for the individual transformers):

  type GameT m = ObjectsT (TextureT (SDLT (OpenGLT m)))

  runGameT :: BaseM m IO => GameT m a -> m a
  runGameT = runOpenGLT . runSDLT . runTextureT . runObjectsT

  myGame :: GameT IO ()
  myGame = do
    loadTexture "blah.jpg"
    addObject $ Player 0 0 0
    addObject $ Enemy 0 0 0
    runContT return . forever $ do
      myGameLoop
      when iWantToAbort $ abort ()

I hope this helps.


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/




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

Message: 6
Date: Wed, 11 Aug 2010 22:35:56 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: [Haskell-beginners] Re: = vs <-
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

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?

No.  You are mixing up monadic computations with normal Haskell
functions.  First of all using "=" you make an equation.  You are not
saving a result, you are just saying that two things are the same.  You
use this to define values and functions:

  (age, sex, location) = (25, Male, "Germany")

  fourthRoot = sqrt . sqrt

You could just as well write '25' whereever your write 'age'.  And you
could just as well write 'sqrt . sqrt' whereever you write 'fourthRoot',
because you defined them to be the same thing.

The '<-' syntax on the other hand is unrelated to functions.  You use it
to give the result of a monadic computation a name.  Example:

  line <- getLine

getLine is a value of type IO String.  Notice that getLine is /not/ a
function.  It's simply a value.  But it's a monadic one, because IO is a
monad, so whenever you use 'do' notation, you can refer to the result of
it by using the '<-' syntax.

Let's go deeper into it:

  content <- readFile "blah.txt"

Now readFile is a function.  It's of the following type:

  readFile :: FilePath -> IO String

Because it is a function you need to apply it to a value of type
FilePath first, yielding, guess what, a simple value.  That value is of
type IO String, so it's a monadic value, an IO computation.  That
computation, when run, has a result.  And by using '<-' you give this
result the name 'content', so you can refer to it.

Again, 'content' is /not/ the result of the readFile function, because
that result is a computation of type IO String.  Let's see the
difference:

  let readBlahTxt = readFile "blah.txt"
  content <- readBlahTxt

This should show the difference more clearly.


Greets,
Ertugrul


-- 
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/




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

Message: 7
Date: Wed, 11 Aug 2010 17:44:42 -0300
From: MAN <[email protected]>
Subject: Re: [Haskell-beginners] global variables
To: prad <[email protected]>
Cc: haskellbeginners <[email protected]>
Message-ID: <1281559482.2154.17.ca...@dy-book>
Content-Type: text/plain; charset="UTF-8"

There are no global variables in Haskell... You can have the names in
the widest namespace, but there's no modification allowed. Whether you
define your connection in the main, or at top (module) level, when you
pass it to a function, it'll be copied.
In your example code your defining a function 'fn' which has this
signature:

  fn :: Connection -> a -> IO b

But the Connection that 'fn' gets is a parameter, not a global.
Ertugrul's approach is the correct one: you define your functions
letting the transformers take care of the plumbing, and the params get
passed around for you.



El mié, 11-08-2010 a las 12:54 -0700, prad escribió:
> i'm curious about the best way to deal with conn to make db connections.
> 
> i define it in main like this, then when i want to use it in a function
> i have to pass it as a parameter:
> 
> main = do
>     conn <- connectPostgreSQL ...
>     fn conn something
> 
> 
> fn conn x = ...
> 
> 
> now conn is a global variable i guess because it is defined in main,
> but perhaps main isn't considered a global namespace? i recall in
> python a global variable could be accessed by functions, but in php
> they can't unless you use "global" within the function.
> 
> so is this the correct way to handle such things? right now it seems
> to be the only way in haskell.
> 




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

Message: 8
Date: Wed, 11 Aug 2010 23:02:19 +0200
From: Benjamin Edwards <[email protected]>
Subject: Re: [Haskell-beginners] = vs <-
To: prad <[email protected]>
Cc: haskellbeginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Not really...

Remember <- is part of the do notation. You use it within monads when using
do, and never outside of them :) Well... list comps as well, but it's much
of a muchness.

On 11 August 2010 22: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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100811/089ec9e6/attachment-0001.html

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

Message: 9
Date: Wed, 11 Aug 2010 22:24:47 +0100
From: Gaius Hammond <[email protected]>
Subject: Re: [Haskell-beginners] global variables
To: prad <[email protected]>
Cc: haskellbeginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes


On 11 Aug 2010, at 20:54, prad wrote:

> i'm curious about the best way to deal with conn to make db  
> connections.
>
> i define it in main like this, then when i want to use it in a  
> function
> i have to pass it as a parameter:
>
> main = do
>    conn <- connectPostgreSQL ...
>    fn conn something
>
>
> fn conn x = ...



I too am interested in this.... Right now the best way to me seems to  
be to use StateT (I am mainly using SQLite).




G






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

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


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

Reply via email to