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. Maybe perform an action (Tim Baumgartner)
2. Re: defining 'init' in terms of 'foldr' (Hein Hundal)
3. Re: Maybe perform an action (Michael Snoyman)
4. Re: Maybe perform an action (Tim Baumgartner)
5. Re: defining 'init' in terms of 'foldr' (Chadda? Fouch?)
6. Re: Exception back trace (Ozgur Akgun)
7. Re: Exception back trace (Michael Snoyman)
8. Re: defining 'init' in terms of 'foldr' (Hein Hundal)
----------------------------------------------------------------------
Message: 1
Date: Sun, 5 Dec 2010 12:23:52 +0100
From: Tim Baumgartner <[email protected]>
Subject: [Haskell-beginners] Maybe perform an action
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Hi Haskellers,
I have another simple question in order to improve my coding style.
I'm using the following function, e.g. when a dialog returned Maybe
NiceStuff:
performMaybe :: Monad m ? Maybe a ? (a ? m b) ? m ()
performMaybe x action = when (isJust x) (action (fromJust x) >> return ())
example use:
main = performMaybe (Just "Hello") print
Now I wonder if others use such a function as well, if it's already
defined in the standard libraries (didn't find it using Hoogle) and
perhaps how to implement it a little nicer.
Regards
Tim
------------------------------
Message: 2
Date: Sun, 5 Dec 2010 03:37:12 -0800 (PST)
From: Hein Hundal <[email protected]>
Subject: Re: [Haskell-beginners] defining 'init' in terms of 'foldr'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
> From: Daniel Fischer <[email protected]>
> Subject: Re: [Haskell-beginners] defining 'init' in terms
> of 'foldr'
> On Saturday 04 December 2010 23:20:51, Paul Higham wrote:
> > Not sure if this thread is still active but I also
> > struggled with this
> > same exercise.? I offer the following solution as
> > a thing to shoot at:
> >
> > myInit :: [a] -> [a]
> > myInit ys = foldr snoc [] $ (\(x:xs) -> xs) $ foldr snoc [] ys
> > ??? ? ? where snoc = (\x xs -> xs ++ [x])
>
> init === reverse . tail . reverse
> only holds for finite lists, for infinite lists xs, reverse
> xs = _|_, but
> init xs = xs.
> Also, it's inefficient, but that's not the point of the
> exercise.
Here is my incomplete beginner solution:
--Start Code
import Data.Maybe
myinit v = fromJust $ foldr f Nothing v
where
f a Nothing = Just []
f a (Just v) = Just (a:v)
--End Code
This worked for the finite lists that I tried, but it did not work for infinite
lists. I was surprised that foldr works with infinite lists. If I set
v = take 5 $ foldr (\x xs -> (x*x):xs) [] [1..]
then the value of v is [1,2,9,16]. Is there another way to create the init
function with foldr that works for infinite lists?
Hein
------------------------------
Message: 3
Date: Sun, 5 Dec 2010 13:40:20 +0200
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] Maybe perform an action
To: Tim Baumgartner <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sun, Dec 5, 2010 at 1:23 PM, Tim Baumgartner
<[email protected]> wrote:
> Hi Haskellers,
>
> I have another simple question in order to improve my coding style.
> I'm using the following function, e.g. when a dialog returned Maybe
> NiceStuff:
>
> performMaybe :: Monad m ? Maybe a ? (a ? m b) ? m ()
> performMaybe x action = when (isJust x) (action (fromJust x) >> return ())
>
> example use:
> main = performMaybe (Just "Hello") print
>
> Now I wonder if others use such a function as well, if it's already
> defined in the standard libraries (didn't find it using Hoogle) and
> perhaps how to implement it a little nicer.
I often times do this as
maybe (return ())
If you want the exact same type signature, you just need to flip it:
flip (maybe $ return ())
Michael
------------------------------
Message: 4
Date: Sun, 5 Dec 2010 12:49:10 +0100
From: Tim Baumgartner <[email protected]>
Subject: Re: [Haskell-beginners] Maybe perform an action
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hi Michael!
Ouch, I really should have some more looks at the standard libraries. Thanks!
Tim
> I often times do this as
>
> maybe (return ())
>
> If you want the exact same type signature, you just need to flip it:
>
> flip (maybe $ return ())
>
> Michael
>
------------------------------
Message: 5
Date: Sun, 5 Dec 2010 13:49:43 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] defining 'init' in terms of 'foldr'
To: Hein Hundal <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Sun, Dec 5, 2010 at 12:37 PM, Hein Hundal <[email protected]> wrote:
>> From: Daniel Fischer <[email protected]>
>> Subject: Re: [Haskell-beginners] defining 'init' in terms
>> of 'foldr'
>> On Saturday 04 December 2010 23:20:51, Paul Higham wrote:
>> > Not sure if this thread is still active but I also
>> > struggled with this
>> > same exercise.? I offer the following solution as
>> > a thing to shoot at:
>> >
>> > myInit :: [a] -> [a]
>> > myInit ys = foldr snoc [] $ (\(x:xs) -> xs) $ foldr snoc [] ys
>> > ??? ? ? where snoc = (\x xs -> xs ++ [x])
>>
>> init === reverse . tail . reverse
>> only holds for finite lists, for infinite lists xs, reverse
>> xs = _|_, but
>> init xs = xs.
>> Also, it's inefficient, but that's not the point of the
>> exercise.
>
> Here is my incomplete beginner solution:
>
> --Start Code
>
> import Data.Maybe
> myinit v = fromJust $ foldr f Nothing v
> ? where
> ? ? ?f a Nothing = Just []
> ? ? ?f a (Just v) = Just (a:v)
>
> --End Code
>
> This worked for the finite lists that I tried, but it did not work for
> infinite lists. ?I was surprised that foldr works with infinite lists. ?If I
> set
>
> v = take 5 $ foldr (\x xs -> (x*x):xs) [] [1..]
>
>
> then the value of v is [1,2,9,16]. ?Is there another way to create the init
> function with foldr that works for infinite lists?
Keeping your idea but being lazier about it :
> myInit v = fromMaybe (error "myInit : empty list") $ foldr f Nothing v
> where
> f x xs = Just (case xs of Nothing -> []; Just ys -> x:ys)
--
Jeda?
------------------------------
Message: 6
Date: Sun, 5 Dec 2010 14:27:58 +0000
From: Ozgur Akgun <[email protected]>
Subject: Re: [Haskell-beginners] Exception back trace
To: [email protected]
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="utf-8"
Great question!
To the best of my knowledge, there is no decent solution. I'd be very
interested in hearing about alternatives and their statuses though.
On 5 December 2010 00:49, Russ Abbott <[email protected]> wrote:
> Hi,
>
> I am in the midst of debugging and got an exception:
>
> *** Exception: Prelude.minimum: empty list
>
>
> That's all it said. There was no information about where the exception
> occurred. Is it possible to ask for more information, preferably including a
> stack trace?
>
> Thanks.
> *
> -- Russ *
>
--
Ozgur Akgun
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20101205/87fa8e9a/attachment-0001.htm>
------------------------------
Message: 7
Date: Sun, 5 Dec 2010 16:36:25 +0200
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] Exception back trace
To: [email protected]
Cc: beginners <[email protected]>
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
You might want to try out monadloc[1] and control-monad-exception[2],
which provide some monadic stack traces via a preprocessor.
Michael
[1] http://hackage.haskell.org/package/monadloc
[2] http://hackage.haskell.org/package/control-monad-exception
On Sun, Dec 5, 2010 at 2:49 AM, Russ Abbott <[email protected]> wrote:
> Hi,
> I am in the midst of debugging and got an exception:
>
> *** Exception: Prelude.minimum: empty list
>
> That's all it said. ?There was no information about where the exception
> occurred. Is it possible to ask for more information, preferably including a
> stack trace?
> Thanks.
>
> -- Russ
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
------------------------------
Message: 8
Date: Sun, 5 Dec 2010 06:51:36 -0800 (PST)
From: Hein Hundal <[email protected]>
Subject: Re: [Haskell-beginners] defining 'init' in terms of 'foldr'
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
I am trying to understand the differences between two
different solutions to the problem of expressing init in
terms of foldr.
The two solutions are called heinInit and chaddiInit.
The heinInit does not work on infinite lists. The
chaddiaiInit function does work on infinite lists.
heinInit v = fromJust $ foldr f Nothing v
where
f a Nothing = Just []
f a (Just v) = Just (a:v)
chaddaiInit v = fromJust $ foldr f Nothing v
where
f x xs = Just (case xs of Nothing -> []; Just ys -> x:ys)
Here is my guess at the "trace" of the chaddaiInit code.
1)
head (chaddaiInit [1..])
2)
head (fromJust $ foldr f Nothing [1..])
3)
head (fromJust $ f 1 (foldr f Nothing [2..]))
4)
head (fromJust $ Just (case (foldr f Nothing [2..]) of
Nothing -> []; Just ys -> 1:ys))
5)
head (case (foldr f Nothing [2..]) of Nothing -> [];
Just ys -> 1:ys)
6)
head (case (f 2 (foldr f Nothing [3..])) of Nothing -> [];
Just ys -> 1:ys)
7)
head (case (Just (case (foldr f Nothing [3..]) of Nothing -> [];
Just ys -> 2:ys)) of Nothing -> []; Just ys -> 1:ys)
8)
head (1:(case (foldr f Nothing [3..]) of Nothing -> [];
Just ys -> 2:ys))
9)
1
Here is my guess at the "trace" of the heinInit code.
myinit v = fromJust $ foldr f Nothing v
where
f a Nothing = Just []
f a (Just v) = Just (a:v)
1)
head (heinInit [1..])
2)
head (fromJust $ foldr f Nothing [1..])
3)
head (fromJust $ f 1 (foldr f Nothing [2..])
4)
head (fromJust $ f 1 (f 2 (fold f Nothing [3..])
5)
head (fromJust $ f 1 (f 2 (f 3 (fold f Nothing [4..])
And it just keeps going. Is this the right idea? Are my
"traces" a reasonable estimate of how Haskell
evaluates those expressions?
Cheers,
Hein
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 30, Issue 7
****************************************