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: cascade of if statements (Sean Perry)
2. Re: Missing termination rule for recursive function
(Oscar Benjamin)
3. Re: Missing termination rule for recursive function
(Daniel Fischer)
4. Re: Missing termination rule for recursive function
(Oscar Benjamin)
5. Re: Trying to write netwire combiner similar to multicast
(Ertugrul S?ylemez)
----------------------------------------------------------------------
Message: 1
Date: Mon, 5 Nov 2012 14:22:09 -0800
From: Sean Perry <[email protected]>
Subject: Re: [Haskell-beginners] cascade of if statements
To: Haskell Beginer <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Oct 31, 2012, at 3:25 AM, Emmanuel Touzery wrote:
> I am checking if a file exists... and then if it exists I'm checking whether
> it's older than two hours from now. It is a bit annoying to have that second
> block in the else (i could make a second function but i think it all belongs
> in this function).
Check out this section of Real World Haskell. It should give you ideas.
http://book.realworldhaskell.org/read/io-case-study-a-library-for-searching-the-filesystem.html
------------------------------
Message: 2
Date: Mon, 5 Nov 2012 23:45:18 +0000
From: Oscar Benjamin <[email protected]>
Subject: Re: [Haskell-beginners] Missing termination rule for
recursive function
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<CAHVvXxSjz+Or+JD_qWMMktHDf7pieVE=1at22pals9mvov+...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On 5 November 2012 21:00, Daniel Fischer
<[email protected]> wrote:
> On Montag, 5. November 2012, 19:53:52, Oscar Benjamin wrote:
>>
>> The Python program used itertools.permutations which is an iterator
>> that yields all permutations of a sequence. Does Haskell have a
>> similar function in it's standard library?
>
> There's permutations in Data.List
>
> Prelude Data.List> permutations [1,2,3]
> [[1,2,3],[2,1,3],[3,2,1],[2,3,1],[3,1,2],[1,3,2]]
I thought as much.
>
> which even works on infinite lists:
>
> Prelude Data.List> map (take 5) . take 5 $ permutations [1 .. ]
> [[1,2,3,4,5],[2,1,3,4,5],[3,2,1,4,5],[2,3,1,4,5],[3,1,2,4,5]]
I'm not sure if I'm ready to understand that one yet...
> permutations [x] = [ y:zs | (y,ys) <- selections [x], zs <- permutations ys]
> ~> [ y:zs | (y,ys) <- [(x,[])], zs <- permutations ys]
> ~> [ x:zs | zs <- permutations []]
> ~> []
>
> since permutations [] = []
>
Ok, I see how that works. So you carefully follow through with how the
comprehension behaves for a concrete simple case until you can see
exactly what you end up with. I guess I know how to do that for
procedural languages but haven't really got my head around how to
follow the execution in Haskell yet.
>> When I changed it to
>>
>> permutations [] = [[]]
>
> That changes the last steps above to
>
> ~> [ x:zs | zs <- permutations []]
> ~> [ x:zs | zs <- [[]] ]
> ~> [ x:[] ]
> ~> [ [x] ]
>
> and the former is not even correct, because there is exactly one permutation
> of an empty list.
I'm trying to convince myself that this is logically necessary but it
still seems consistent in my mind that the set of permutations of an
empty list is empty. I guess maybe if you posit that a sequence should
always be contained by the set of its permutations then you reach this
conclusion.
>
> Trace the execution of very simple cases (empty lists, singleton lists, lists
> with two elements) by hand with pencil and paper. That's the most instructive
> and fruitful way.
>
> Check the results of simple cases against what you know the result ought to
> be.
I see what you mean now.
> Single-step through the evaluation of simple cases in the ghci debugger if
> necessary.
I need to work out how to use this. I've just found why I was unable
to use it before: it only works if the file is interpreted. The quick
fix was to delete all files created by the compiler. Is there a way
that I can tell ghci to just ignore those files and load my program as
interpreted for debugging?
Thanks again,
Oscar
------------------------------
Message: 3
Date: Tue, 6 Nov 2012 00:53:27 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Missing termination rule for
recursive function
To: Oscar Benjamin <[email protected]>
Cc: [email protected]
Message-ID:
<cakqtcbuy1fg5wkq8zwmuhtrdyqw84qw3rsbfzdiou2yqdg6...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
On Tue, Nov 6, 2012 at 12:45 AM, Oscar Benjamin
<[email protected]>wrote:
> On 5 November 2012 21:00, Daniel Fischer wrote
>
> > and the former is not even correct, because there is exactly one
> permutation
> > of an empty list.
>
> I'm trying to convince myself that this is logically necessary but it
> still seems consistent in my mind that the set of permutations of an
> empty list is empty. I guess maybe if you posit that a sequence should
> always be contained by the set of its permutations then you reach this
> conclusion.
>
A permutation of a set is a bijection from the set to itself. There is one
map from the empty set to itself, the empty map, that is a bijection.
The number of permutations of a set of n elements is n!, and 0! = 1 should
make you expect that there is one permutation of an empty set.
>
> >
> > Trace the execution of very simple cases (empty lists, singleton lists,
> lists
> > with two elements) by hand with pencil and paper. That's the most
> instructive
> > and fruitful way.
> >
> > Check the results of simple cases against what you know the result ought
> to
> > be.
>
> I see what you mean now.
>
> > Single-step through the evaluation of simple cases in the ghci debugger
> if
> > necessary.
>
> I need to work out how to use this. I've just found why I was unable
> to use it before: it only works if the file is interpreted. The quick
> fix was to delete all files created by the compiler. Is there a way
> that I can tell ghci to just ignore those files and load my program as
> interpreted for debugging?
>
> You can
>
ghci> :load *ModuleName
to get it interpreted.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121106/4eb4965d/attachment-0001.htm>
------------------------------
Message: 4
Date: Mon, 5 Nov 2012 23:57:14 +0000
From: Oscar Benjamin <[email protected]>
Subject: Re: [Haskell-beginners] Missing termination rule for
recursive function
To: Jay Sulzberger <[email protected]>
Cc: [email protected]
Message-ID:
<cahvvxxr57ya+6qfxgezsuos1lyz4cek6r8evan2lodfm3pl...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On 5 November 2012 21:27, Jay Sulzberger <[email protected]> wrote:
> On Mon, 5 Nov 2012, Oscar Benjamin <[email protected]> wrote:
>
>> -- Select each item and remainder from a sequence
>> selections :: [a] -> [(a, [a])]
>> selections [] = []
>> selections (x:xs) = (x,xs) : [ (y,x:ys) | (y,ys) <- selections xs ]
>>
>> -- Permutations of a sequence
>> permutations :: [a] -> [[a]]
>> permutations xs = [ y:zs | (y,ys) <- selections xs, zs <- permutations ys
>> ]
>>
>
> Assuming selections is correct, we have
>
> permutations ["a"] ~> the list of all lists of form "a":(something that
> lies in permutations [])
>
> So what is the value of permutations []? It is the list of all things of
> form
>
> y:zs
>
> such that
>
> (y,ys) lies in selections xs and zs lies in permutations ys
When you rephrase in set terminology like this it becomes easier to
understand. I was thinking of it all in terms of loops before.
> where xs = []. But there are no such things. And so the list of
> sll such things is the empty list [].
>
> What is perhaps confusing is that, at this juncture, one tends to
> think that
>
> y:zs
>
> must really be
>
> y:[]
>
> but it is not.
That's exactly what confused me. It doesn't confuse me when it's laid
out like this but in the list comprehension it did.
Thanks for your help.
Oscar
------------------------------
Message: 5
Date: Tue, 6 Nov 2012 02:44:00 +0100
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] Trying to write netwire combiner
similar to multicast
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Nathan H?sken <[email protected]> wrote:
> > Also your interface seems very unsafe to me. I suggest the
> > following interface instead:
> >
> > shrinking :: (Monad m) => [Wire e m a b] -> Wire e m a [b]
> >
> > Then normally 'a' could be something like Map K A.
>
> That would mean, the individual wires have to know there own id?!?
> Mmmh, I will try to keep this bookkeeping out of the wires with this
> interface:
>
> shrinking :: (Monad m) => [Wire e m a b] -> Wire e m (Map Int a)
> (Int,b)
>
> shrinking will assign the ids to the wires and returns them with the
> result. I will see where this gets me ... :).
The problem with such an interface is the inflexibility. Notice that
removing a subwire will change the numbering of all subsequent wires.
The interface I suggested follows this basic idea:
shrinking ::
(Monad m)
=> [(a' -> a, Wire e m a b)]
-> Wire e m a' b
The reasoning is that this way you disconnect the individual values from
the positions in the subwire list. This will also make writing the
combinator a bit simpler. If you will here is an interesting
alternative:
data Subwire e m a b =
forall a'. Subwire (a -> a') (Wire e m a' b)
shrinking :: (Monad m) => [Subwire e m a b] -> Wire e m a b
It doesn't buy you much except for some minor additional type safety,
the cleaner type signature and the opportunity to use a crazy type
system extension. =)
By the way, you can find this style all throughout the Netwire library.
See for example 'context' and 'require'. You may also find that the
various context combinators from Control.Wire.Trans.Combine could fit
your need, although they do not multicast.
Final note: I renamed your 'manager' combinator to 'shrinking' to save
you from future name clashes, because I have planned to write a more
general manager combinator.
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20121106/374552f9/attachment-0001.pgp>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 53, Issue 8
****************************************