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:  Re: testing and the culture of Haskell (Adrian Adshead)
   2. Re:  Re: testing and the culture of Haskell (Isaac Dupree)
   3.  Backtracking (legajid)
   4. Re:  Re: testing and the culture of Haskell (Brent Yorgey)
   5. Re:  Backtracking (Daniel Fischer)
   6.  Re: Re: testing and the culture of Haskell (Maciej Piechotka)
   7.  Re: Re: testing and the culture of Haskell (Adrian Adshead)


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

Message: 1
Date: Fri, 22 Jan 2010 20:59:57 +0000 (GMT)
From: Adrian Adshead <[email protected]>
Subject: Re: [Haskell-beginners] Re: testing and the culture of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

>All Haskell functions are pure without exception.  For example:
>
>greet :: String -> IO ()
>greet name = putStrLn $ "Hello, "++name
>
>This is a pure function from String to IO ().  This function (like all
>Haskell functions) has no side effects.  Its return value of type IO ()
>merely _represents_ an IO action.  The runtime system knows how to act
>on this representation.
>
>This also means that there is no such thing in Haskell as marking a
>function as side-effecting.
>
>This distinction may be subtle, but it's important.
>
>
>Steve

Steve,

Please could you clarify this for me since you are making exactly
the opposite assertion than I have understood.

I am confused by you stating "All Haskell functions are pure
without exception.".

Pure functions have no impact on 'anything'. They take input
parameters (which they don't change) and return exactly the
same result whenever the same input parameters are given.

>greet :: String -> IO ()
>greet name = putStrLn $ "Hello, "++name

This example you gave is not a pure function since it does have
the side effect that the screen is changed by outputting the string
"Hello, " and the name passed in.

As I understand it the IO in the type signature is the programmers
indication to the compiler that the function is not guaranteed to
be side effect free.

add_pure :: Integer -> Integer
add_pure x = x + 5

add_impure :: Integer -> IO Integer
add_impure x = return (x + 5)

add_pure is clearly a pure function. add_impure while it is
totally side effect free and therefore fulfills the definition
of purity, is impure as far as the compiler is concerned since
I (the programmer) have told the compiler that I do not guarantee
that the function is pure.

Please let me know where I am misunderstanding purity.

Many thanks

Adrian.



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100122/55f48277/attachment-0001.html

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

Message: 2
Date: Fri, 22 Jan 2010 16:16:44 -0500
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Re: testing and the culture of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

Adrian Adshead wrote:
> add_impure :: Integer -> IO Integer
> add_impure x = return (x + 5)
> 
> add_pure is clearly a pure function. add_impure while it is
> totally side effect free and therefore fulfills the definition
> of purity, is impure as far as the compiler is concerned since
> I (the programmer) have told the compiler that I do not guarantee
> that the function is pure.
> 
> Please let me know where I am misunderstanding purity.

Because, actually, (IO Integer) represents an action but it'll only be 
executed if it's sequenced somewhere under main. e.g.

main :: IO ()
main = do
   let io_action = something_impure x y z
   -- nothing has happened yet!
   io_action
   io_action
   -- now we've executed the impure action *twice*! (But some of
   -- the pure computation based on x y and z that determined
   -- what actions io_action should be, might be shared)

(a simplistic example... io_action can do different things each time 
it's executed based on any reason it can execute in IO, such as, reading 
the system clock)

-Isaac


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

Message: 3
Date: Fri, 22 Jan 2010 23:33:51 +0100
From: legajid <[email protected]>
Subject: [Haskell-beginners] Backtracking
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
i'm writing a Sudoku solver -a very new idea ;) - , using brute force.
The challenge for me is to keep track of all tries and go back and forward.

My general idea is :
make a list of all cells with value 0
For each of these cells, try values 1 to 9.
If a value matches (that is not used in cells on the same line, column 
or square) then proceed the same way with the next empty cell.
If a value doesn't match, get back to the preceding cell and take the 
next value available : here is my problem, i don't know how to go back.

The calcul function, invoked once, creates the list of empty cells, then 
calls calcul'
Calcul' creates the list of possible values, then calls calcul'' with 
the list.
Calcul'' tries to apply each possible value.If ok, it then calls calcul' 
for the remaining empty cells list. If not, calls itself with the next 
value for the cell.
The calls for different cells are stacked.
In case of backtrack, i must re_call calcul'' with the preceding cell 
number, going on with its list of values, at the point it was stopped.
How to write this ?

Thanks for your help.

Didier

-------------- next part --------------
A non-text attachment was scrubbed...
Name: sud2.hs
Type: text/x-haskell
Size: 3953 bytes
Desc: not available
Url : 
http://www.haskell.org/pipermail/beginners/attachments/20100122/5a62156f/sud2-0001.bin

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

Message: 4
Date: Fri, 22 Jan 2010 17:30:31 -0500
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Re: testing and the culture of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Jan 22, 2010 at 08:59:57PM +0000, Adrian Adshead wrote:
>
> I am confused by you stating "All Haskell functions are pure
> without exception.".
> 
> Pure functions have no impact on 'anything'. They take input
> parameters (which they don't change) and return exactly the
> same result whenever the same input parameters are given.
> 
> >greet :: String -> IO ()
> >greet name = putStrLn $ "Hello, "++name
> 
> This example you gave is not a pure function since it does have
> the side effect that the screen is changed by outputting the string
> "Hello, " and the name passed in.

This is indeed a subtle point, but this example actually *is* a pure
function.  When you pass it a String, it returns a value of type IO
(), which is a *description* of a (side-effecting) computation to be
carried out.  Every time you pass it the same String, it will give you
back the exact same *description* of a computation.

It is only when that description is handed off to the runtime system
(usually by including it somewhere in the special IO () value called
'main') that it gets run and causes actual effects.

-Brent


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

Message: 5
Date: Sat, 23 Jan 2010 00:09:42 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Backtracking
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="utf-8"

Am Freitag 22 Januar 2010 23:33:51 schrieb legajid:
> Hi,
> i'm writing a Sudoku solver -a very new idea ;) - , using brute force.
> The challenge for me is to keep track of all tries and go back and
> forward.
>
> My general idea is :
> make a list of all cells with value 0
> For each of these cells, try values 1 to 9.
> If a value matches (that is not used in cells on the same line, column
> or square) then proceed the same way with the next empty cell.
> If a value doesn't match, get back to the preceding cell and take the
> next value available : here is my problem, i don't know how to go back.
>
> The calcul function, invoked once, creates the list of empty cells, then
> calls calcul'
> Calcul' creates the list of possible values, then calls calcul'' with
> the list.
> Calcul'' tries to apply each possible value.If ok, it then calls calcul'
> for the remaining empty cells list. If not, calls itself with the next
> value for the cell.
> The calls for different cells are stacked.
> In case of backtrack, i must re_call calcul'' with the preceding cell
> number, going on with its list of values, at the point it was stopped.
> How to write this ?

You get automatic backtracking if you calculate the list of all solutions 
(just in case the starting grid is too underdetermined, be sure to only 
call "take k $ solutions startGrid" for suitably small values of k).

solutions :: Grid -> [Grid]
solutions g = 
  case libres g of
    [] -> return g       -- fini
    (libre:_) -> do
      possibilité <- valeurs libre g
      solutions (place possibilité libre g)

Check if there are any free cells left. If not, we've found a solution.
If there are, try all possibilities for the first free cell and solve from 
then on. Invalid (or wrong) choices lead to an empty list of possibilities 
some time later, in which case that branch is aborted.

For performance, you can see that a simple optimisation is sorting the list 
of free cells by the number of possibilities they allow, so a branch isn't 
kept alive after it is clear that there are no possibilities to fill the 
last cell.

>
> Thanks for your help.
>
> Didier



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

Message: 6
Date: Sat, 23 Jan 2010 00:31:42 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Re: testing and the culture of
        Haskell
To: [email protected]
Message-ID: <1264206701.5563.13.ca...@picard>
Content-Type: text/plain; charset="UTF-8"

On Fri, 2010-01-22 at 20:59 +0000, Adrian Adshead wrote:
> >All Haskell functions are pure without exception.  For example:
> >
> >greet :: String -> IO ()
> >greet name = putStrLn $ "Hello, "++name
> >
> >This is a pure function from String to IO ().  This function (like all
> >Haskell functions) has no side effects.  Its return value of type IO ()
> >merely _represents_ an IO action.  The runtime system knows how to act
> >on this representation.
> >
> >This also means that there is no such thing in Haskell as marking a
> >function as side-effecting.
> >
> >This distinction may be subtle, but it's important.
> >
> >
> >Steve
> 
> Steve,
> 
> Please could you clarify this for me since you are making exactly
> the opposite assertion than I have understood.
> 
> I am confused by you stating "All Haskell functions are pure
> without
>  exception.".
> 
> Pure functions have no impact on 'anything'. They take input
> parameters (which they don't change) and return exactly the
> same result whenever the same input parameters are given.
> 
> >greet :: String -> IO ()
> >greet name = putStrLn $ "Hello, "++name
> 
> This example you gave is not a pure function since it does have
> the side effect that the screen is changed by outputting the string
> "Hello, " and the name passed in.
> 
> 

greatAdrian :: String
greetAdrian = let x = greet "Adrian"
              in x `seq` f x

greet can be consider a pure function and value IO () is evaluated by
seq. IO () represents an action(s) not execution of action(s). If f of x
does not use any tricks nothing will be printed.

IO a value it can be:
- cast unsafely into a. However I guess we omit this shame for a moment
- binded with other action. But the resultant type is now again IO b. So
we still get a something 
- returned as main. Then we might consider whole Haskell program as
metalanguage which returns single thing - other program. In similar way
as:

type PythonProgram = String
main :: PythonProgram
main = "print \"Hello World\""

is pure add_impure is pure. What we do with the result is other thing.

Regards




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

Message: 7
Date: Sat, 23 Jan 2010 03:34:51 +0000 (GMT)
From: Adrian Adshead <[email protected]>
Subject: [Haskell-beginners] Re: Re: testing and the culture of
        Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"

OK, so I get the idea that all functions are pure because they
just return actions.

So as I understand it now...

All functions are pure.
The evaluation of all functions not declared as IO results in no side effects.
The evaluation of all functions declared as IO may or may not have side effects.

But how can the function greetAdrian from the example below not be an IO
operation?




From: Maciej Piechotka <uzytkownik2 <at> gmail.com>

Subject: Re: Re: testing and the culture of Haskell

Newsgroups: gmane.comp.lang.haskell.beginners

Date: 2010-01-23 00:31:42 GMT
 (2 hours and 52 minutes ago)

On Fri, 2010-01-22 at 20:59 +0000, Adrian Adshead wrote:
> >All Haskell functions are pure without exception.  For example:
> >
> >greet :: String -> IO ()
> >greet name = putStrLn $ "Hello, "++name
> >
> >This is a pure function from String to IO ().  This function (like all
> >Haskell functions) has no side effects.  Its return value of type IO ()
> >merely _represents_ an IO action.  The runtime system knows how to act
> >on this representation.
> >
> >This also means that there is no such thing in Haskell as marking a
> >function as side-effecting.
> >
> >This distinction may be subtle, but it's important.
> >
> >
> >Steve
> 
> Steve,
> 
> Please could you clarify this for me since you are making exactly
> the opposite assertion than I have understood.
> 
> I am confused by you stating "All Haskell functions are pure
> without
>  exception.".
> 
> Pure functions have no impact on 'anything'. They take input
> parameters (which they don't change) and return exactly the
> same result whenever the same input parameters are given.
> 
> >greet :: String -> IO ()
> >greet name = putStrLn $ "Hello, "++name
> 
> This example you gave is not a pure function since it does have
> the side effect that the screen is changed by outputting the string
> "Hello, " and the name passed in.
> 
> 

greatAdrian :: String
greetAdrian = let x = greet "Adrian"
              in x `seq` f x

greet can be consider a pure function and value IO () is evaluated by
seq. IO () represents an action(s) not execution of action(s). If f of x
does not use any tricks nothing will be printed.

IO a value it can be:
- cast unsafely into a. However I guess we omit this shame for a moment
- binded with other action. But the resultant type is now again IO b. So
we still get a something 
- returned as main. Then we might consider whole Haskell program as
metalanguage which returns single thing - other program. In similar way
as:

type PythonProgram = String
main :: PythonProgram
main = "print \"Hello World\""

is pure add_impure is pure. What we do with the result is other thing.

Regards




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100122/bda4e14b/attachment.html

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

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


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

Reply via email to