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: "pure" versus "impure" code ([email protected])
2. Is there a place where we can have a discussion on the
merits of various GUI's & various graphics? And then vote? (KC)
3. Re: Is there a place where we can have a discussion on the
merits of various GUI's & various graphics? And then vote? (KC)
4. Re: I don't understand mapM in mapM id (Just 1, Nothing,
Just 3) (Arlen Cuss)
5. Load functions from broken source code files?
(Christopher Howard)
6. Re: Load functions from broken source code files?
(Lyndon Maydwell)
7. "In several chapters there is a penetrating conceptual
analysis of the matter at hand, on which a superstructure of code
is erected." (KC)
8. Extension for "Pearls of Functional Algorithm Design" by
Richard Bird, 2010, page 25 #Haskell (KC)
----------------------------------------------------------------------
Message: 1
Date: Fri, 20 May 2011 11:59:15 -0400
From: "[email protected]" <[email protected]>
Subject: Re: [Haskell-beginners] "pure" versus "impure" code
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Paul Sargent <[email protected]> wrote:
>On 19 May 2011, at 21:12, Costello, Roger L. wrote:
>> My understanding is that:
>>
>> - Pure code is code that does no I/O
>>
>> - Impure code is code that does I/O
>
>I don't particularly like that definition. It's true, but the
>definition of I/O has to be very board.
I would say so! I'm used to "Pure functions don't change state", where IO is
pretty much a change of state by definition.
>The definition I use is that a pure function is one that
> - takes input only from it's parameters.
> - outputs only via it's return value.
Not quite mine, but I think it's better for reasoning about Haskell code. Maybe
- I have a question at the end.
>If x is an external variable in memory
>Pure : f(b,c) = b + c
>Impure: f(b) = b + x; (violates first rule - Uses value in
>x)
Right, but ... see the question at the end.
>Impure: f(b,c) = 1 ; x = b + c; (violates second rule - Changes value
>in x)
>
>Pure functions can combine to make larger pure functions. Impure
>functions combined with pure functions make larger impure functions.
>
>> Also, it is my understanding that good software design is to
>isolate/separate the impure code from the pure code. Is that correct?
>As the result of pure code is dependent only on it's inputs you can
>make statements like:
> f(1,2) is always is equal to f(1,2) at any time
>...without knowing what function f() is. That's quite powerful, and can
>be useful in optimisation.
Among other places! The point of this kindof distinction is that it's easier to
reason about "pure" code - because you can make statements like that about them.
>Another way of phrasing it - A pure piece of code causes no
>side-effects, and it is not effected by other code's side-effects. This
>property is very useful in parallel computations.
Yup. And reasoning about parallel computation is hard enough as it is.
>Separating pure code out can be useful, but most programs in most
>languages don't. That's mainly due to the use of shared state variables
>between functions. Object orientation is inherently impure as every
>object has retained state, modified and used by the methods. This isn't
>necessarily a bad thing. At some point all programs have to be impure
>otherwise they'll always calculate the same result.
>
>> Does that principle apply to all programming languages, or just
>Haskell?
>
>Yes, it applies to all languages.
But in different ways to different languages.
Look at the "doesn't change state" definition - basically, yours without the
"depends on state that other code can change" restriction. That means that you
can safely ignore the function when analyzing the calling code - you know it
doesn't change any state. If the langauge is greedy, an optimizer can safely
inline the function at that point. If the language is lazy, then the optimizer
can't do the inlining, because the external state may change between the
function invocation and actually evaluating the function, which would change
the results of the call.
In other words, in a greedy language you might get more leverage out of the
looser definition of "pure."
In your example, x - a value that's not a paramenter - is conventionally known
as a free variable. IIUC, in Haskell, names can't change their value unless
they're in a monad. Which means the two different definitions you gave for
"pure" describe different types of functions: A function which takes input only
from it's parameters has no free variables. On the other hand, a function which
uses free variables that aren't in a monad can't be affected by other code's
side-effects, even though it takes input other than it's parameters (at least
if you have sane scoping rules).
Hence the question: which of these is more useful for Haskell?
--
Sent from my Android tablet with K-9 Mail. Please excuse my brevity.
------------------------------
Message: 2
Date: Fri, 20 May 2011 12:20:53 -0700
From: KC <[email protected]>
Subject: [Haskell-beginners] Is there a place where we can have a
discussion on the merits of various GUI's & various graphics? And then
vote?
To: haskell-cafe <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
:)
--
--
Regards,
KC
------------------------------
Message: 3
Date: Fri, 20 May 2011 12:32:40 -0700
From: KC <[email protected]>
Subject: Re: [Haskell-beginners] Is there a place where we can have a
discussion on the merits of various GUI's & various graphics? And then
vote?
To: haskell-cafe <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
So we can get this "show" on the road. Pun intended. :D
On Fri, May 20, 2011 at 12:20 PM, KC <[email protected]> wrote:
> :)
>
--
--
Regards,
KC
------------------------------
Message: 4
Date: Sat, 21 May 2011 10:25:02 +1000
From: Arlen Cuss <[email protected]>
Subject: Re: [Haskell-beginners] I don't understand mapM in mapM id
(Just 1, Nothing, Just 3)
To: Kees Bleijenberg <[email protected]>
Cc: [email protected]
Message-ID: <1305937502.2636.7.camel@asu>
Content-Type: text/plain; charset="utf-8"
> mapM id [Just 1, Just 2, Just 3]
> result: Just [1,2,3]
>
> mapM :: (a -> m b) -> [a] -> m [b]
> So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2,
> Just 3] and b = Int and m = Maybe. So id is :: Maybe Int -> Maybe Int
Right! So note here that 'm' is Maybe and 'b' is 'Int', thus mapM's
return value is 'm [b]', i.e. 'Maybe [Int]'. The implication is that it
somehow yields a Maybe of [Int], but no Maybe Int.
> mapM id [Just 1, Nothing, Just 3]
> result: Nothing.
> My first guess for the result: Just [Just 1, Nothing, Just 3]
This is contingent of the semantics of the Maybe monad. First, mapM's
definition:
> mapM f as = sequence (map f as)
So the list is mapped onto the (monadic!) function, then sequenced:
> sequence :: Monad m => [m a] -> m [a]
> sequence = foldr mcons (return [])
> where
> mcons p q = p >>= \x -> q >>= \y -> return (x : y)
Note that consecutive values are bound, so seeing this example should
clarify why a single Nothing causes mapM to return Nothing for the lot:
> Just 1 >> Just 2
Just 2
> Nothing >> Just 2
Nothing
>
This falls simply out of Maybe's Monad instance definition for bind:
> instance Monad Maybe where
> (Just x) >>= k = k x
> Nothing >>= k = Nothing
HTH.
Arlen
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110521/8647b82c/attachment-0001.pgp>
------------------------------
Message: 5
Date: Fri, 20 May 2011 19:44:43 -0800
From: Christopher Howard <[email protected]>
Subject: [Haskell-beginners] Load functions from broken source code
files?
To: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Is there some way in GHCi to load a single function from a broken source
code file? E.g, you're writing a program, the main function is
incomplete, and half the other functions are broken, but you are
developing one function and you just want to load in the current version
of that, reload it after some editing, and so forth.
I can see how normally that would not be a good idea, but I was
wondering if there was some hackish way to do it that was fairly easy
and quick.
--
frigidcode.com
theologia.indicium.us
------------------------------
Message: 6
Date: Sat, 21 May 2011 11:50:30 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] Load functions from broken source
code files?
To: Christopher Howard <[email protected]>
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
You should be able to do this with the help of your IDE.
For example, I've written a vim plugin to just load the modules
referenced rather than the functions for a file I'm working on. You
could do the same for a visual selection of code. Also, I believe that
emacs can load arbitrary code sections into ghci for you.
On Sat, May 21, 2011 at 11:44 AM, Christopher Howard
<[email protected]> wrote:
> Is there some way in GHCi to load a single function from a broken source
> code file? E.g, you're writing a program, the main function is
> incomplete, and half the other functions are broken, but you are
> developing one function and you just want to load in the current version
> of that, reload it after some editing, and so forth.
>
> I can see how normally that would not be a good idea, but I was
> wondering if there was some hackish way to do it that was fairly easy
> and quick.
>
> --
> frigidcode.com
> theologia.indicium.us
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 7
Date: Fri, 20 May 2011 20:59:57 -0700
From: KC <[email protected]>
Subject: [Haskell-beginners] "In several chapters there is a
penetrating conceptual analysis of the matter at hand, on which a
superstructure of code is erected."
To: haskell-cafe <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Interesting comment on the book "The Fun of Programming" in JOOP.
"In several chapters there is a penetrating conceptual analysis of the
matter at hand, on which a superstructure of code is erected."
--
--
Regards,
KC
------------------------------
Message: 8
Date: Fri, 20 May 2011 21:09:01 -0700
From: KC <[email protected]>
Subject: [Haskell-beginners] Extension for "Pearls of Functional
Algorithm Design" by Richard Bird, 2010, page 25 #Haskell
To: haskell-cafe <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Extension for "Pearls of Functional Algorithm Design" by Richard Bird,
2010, page 25 #Haskell
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
module SelectionProblem where
import Data.Array
import Data.List
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Question: is there a way to get the type signature as the following:
-- smallest :: (Ord a) => Int -> [Array Int a] -> a
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Works on 2 finite ordered disjoint sets represented as sorted arrays.
smallest :: (Ord a) => Int -> (Array Int a, Array Int a) -> a
smallest k (xa,ya) =
search k (xa,ya) (0,m+1) (0,n+1)
where
(0,m) = bounds xa
(0,n) = bounds ya
-- Removed some of the "indexitis" at the cost of calling another function.
search :: (Ord a) => Int -> (Array Int a, Array Int a) -> (Int,Int) ->
(Int,Int) -> a
search k (xa,ya) (lx,rx) (ly,ry)
| lx == rx = ya ! (k+ly)
| ly == ry = xa ! (k+lx)
| otherwise = case (xa ! mx < ya ! my) of
(True) -> smallest2h k (xa,ya) ((lx,mx,rx),(ly,my,ry))
(False) -> smallest2h k (ya,xa) ((ly,my,ry),(lx,mx,rx))
where
mx = (lx+rx) `div` 2
my = (ly+ry) `div` 2
-- Here the sorted arrays are in order by their middle elements.
-- Only cutting the leading or trailing array by half.
-- Here xa is the first array and ya the second array by their middle elements.
smallest2h :: (Ord a) => Int -> (Array Int a, Array Int a) ->
((Int,Int,Int),(Int,Int,Int)) -> a
smallest2h k (xa,ya) ((lx,mx,rx),(ly,my,ry)) =
case (k<=mx-lx+my-ly) of
(True) -> search k (xa,ya) (lx,rx) (ly,my)
(False) -> search (k-(mx-lx)-1) (xa,ya) (mx+1,rx) (ly,ry)
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Works on 3 finite ordered disjoint sets represented as sorted arrays.
smallest3 :: (Ord a) => Int -> (Array Int a, Array Int a, Array Int a) -> a
smallest3 k (xa,ya,za) =
-- On each recursive call the order of the arrays can switch.
search3 k (xa,ya,za) (0,bx+1) (0,by+1) (0,bz+1)
where
(0,bx) = bounds xa
(0,by) = bounds ya
(0,bz) = bounds za
-- Removed some of the "indexitis" at the cost of calling another function.
search3 :: (Ord a) => Int -> (Array Int a, Array Int a, Array Int a) ->
(Int,Int) -> (Int,Int) -> (Int,Int) -> a
search3 k (xa,ya,za) (lx,rx) (ly,ry) (lz,rz)
| lx == rx && ly == ry = za ! (k+lz)
| ly == ry && lz == rz = xa ! (k+lx)
| lx == rx && lz == rz = ya ! (k+ly)
| lx == rx = search k (ya,za) (ly,ry) (lz,rz)
| ly == ry = search k (xa,za) (lx,rx) (lz,rz)
| lz == rz = search k (xa,ya) (lx,rx) (ly,ry)
| otherwise = case (xa ! mx < ya ! my, xa ! mx < za ! mz, ya ! my
< za ! mz) of
(True, True, True) -> smallest3h k (xa,ya,za)
((lx,mx,rx),(ly,my,ry),(lz,mz,rz)) -- a<b<c
(True, True, False) -> smallest3h k (xa,za,ya)
((lx,mx,rx),(lz,mz,rz),(ly,my,ry)) -- a<c<b
(False, True, True) -> smallest3h k (ya,xa,za)
((ly,my,ry),(lx,mx,rx),(lz,mz,rz)) -- b<a<c
(False, False, True) -> smallest3h k (ya,za,xa)
((ly,my,ry),(lz,mz,rz),(lx,mx,rx)) -- b<c<a
(True, False, False) -> smallest3h k (za,xa,ya)
((lz,mz,rz),(lx,mx,rx),(ly,my,ry)) -- c<a<b
(False, False, False) -> smallest3h k (za,ya,xa)
((lz,mz,rz),(ly,my,ry),(lx,mx,rx)) -- c<b<a
where
mx = (lx+rx) `div` 2
my = (ly+ry) `div` 2
mz = (lz+rz) `div` 2
-- Here the sorted arrays are in order by their middle elements.
-- Only cutting the leading or trailing array by half.
-- Here xa is the first array, ya the second array, and za the third
array by their middle elements.
smallest3h :: (Ord a) => Int -> (Array Int a, Array Int a, Array Int a) ->
((Int,Int,Int),(Int,Int,Int),(Int,Int,Int)) -> a
smallest3h k (xa,ya,za) ((lx,mx,rx),(ly,my,ry),(lz,mz,rz)) =
case (k<=mx-lx+my-ly+mz-lz) of
(True) -> search3 k (xa,ya,za) (lx,rx) (ly,ry) (lz,mz)
(False) -> search3 (k-(mx-lx)-1) (xa,ya,za) (mx+1,rx) (ly,ry) (lz,rz)
--
--
Regards,
KC
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 43
*****************************************