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:  Iterating a monadic action with memoization (Brent Yorgey)
   2.  Question: Data Type for user selection (Hartmut)
   3. Re:  Question: Data Type for user selection (Brandon Allbery)
   4. Re:  the first argument of take is an Int and not Integral..
      (Dean Herington)
   5. Re:  Question: Data Type for user selection (Ertugrul Soeylemez)
   6.  flatten (Sunil S Nandihalli)
   7. Re:  flatten (Michael Snoyman)
   8. Re:  flatten (Sunil S Nandihalli)
   9. Re:  flatten (Sunil S Nandihalli)


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

Message: 1
Date: Mon, 15 Aug 2011 11:55:06 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Iterating a monadic action with
        memoization
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Mon, Aug 15, 2011 at 09:00:19AM +0100, Tim Cowlishaw wrote:
> Hi all,
> 
> I was wondering if any of you knew whether the following was possible,
> (I discussed this a little on #haskell at the weekend but didn't quite
> get to the bottom of it):
> 
> Say I have a monadic action:
> 
> > f :: a -> m a
> 
> and an initial value of type a.
> 
> I'd like to iterate this action and collect the results of each
> iteration in a list, as with 'iterate' on normal functions:
> 
> > iterate (>>= f) . return $ a
> 
> However, I would also like to memoize the intermediate results of the
> iteration, such that the entire iteration runs in O(n) time rather
> than O(n^2). This also implies a slight semantic difference, as
> side-effecting or non-deterministic actions will not be repeated in
> each iteration, while maintaining the laziness of the iteration
> function. Is it possible to do this?

How about this?

  iterateM :: Monad m => (a -> m a) -> a -> m [a]
  iterateM f a = (a:) `liftM` (f a >>= iterateM f)

-Brent



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

Message: 2
Date: Tue, 16 Aug 2011 01:35:57 +0200
From: Hartmut <[email protected]>
Subject: [Haskell-beginners] Question: Data Type for user selection
To: [email protected]
Message-ID:
        <CAFz=thgehqodtl278la1_v17cqpzft5t0x7ys6xd1brjyjy...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Haskellers,

in the following testing code I want to model user selection criterias.

Explanation of the requirement:
- BasicSelect represents a single value (in the beginning: Number or String)
- ExtendedSelect is a single value plus a sign if selection is meant
inclusive or exclusive
- MultipleSelections is a set of ExtendedSelect's

I have some problems with that:
- The BasicSelect shall only accept SelectionNum's or SelectionStr's.
Currently it accepts anything?
- The MultipleSelections shall only accept EmptySel or references to
ExtendedSelect's.

Can anybody give me a hint how do this? Does this work with plain data
types?
Hartmut


module SelectionCriterias where

data InclusiveOrExclusive = Inclusive | Exclusive

data BasicSelect a =  NumberSelect a | StringSelect a
data Num a => NumberSelect a = SelectionNum a
data Show a => StringSelect a = SelectionStr a

data ExtendedSelect a = ExtendedSelect {
  basicSel :: BasicSelect a,
  inclOrExcl :: InclusiveOrExclusive
}

data MultipleSelections a = EmptySel | SingleSel a | MultipleSel [a]

-----------------------------------------------------------------------------
-- Examples/Usage:

-- e1 :: BasicSelect Integer
e1 = NumberSelect 100
e2 = NumberSelect 110

-- e2 :: BasicSelect [Char]
e3 = StringSelect "test3"
e4 = StringSelect "test4"
e5 = StringSelect "test5"


-- f1 :: ExtendedSelect Integer
f1 = ExtendedSelect { basicSel = e1, inclOrExcl = Inclusive }
f2 = ExtendedSelect { basicSel = e2, inclOrExcl = Exclusive }

-- f2 :: ExtendedSelect [Char]
f3 = ExtendedSelect { basicSel = e3, inclOrExcl = Exclusive }


-- multi1 :: MultipleSelections a
multi1 = EmptySel

-- multi2 :: MultipleSelections (ExtendedSelect Integer)
multi2 = SingleSel f1

-- multi3 :: MultipleSelections (ExtendedSelect Integer)
multi3 = MultipleSel [f1,f2]

-- e.g. shall not be valid - because Bool not supported
multi4 = MultipleSel [True, False]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110816/9d7f58ea/attachment-0001.htm>

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

Message: 3
Date: Mon, 15 Aug 2011 23:43:11 -0400
From: Brandon Allbery <[email protected]>
Subject: Re: [Haskell-beginners] Question: Data Type for user
        selection
To: Hartmut <[email protected]>
Cc: [email protected]
Message-ID:
        <cakfcl4wjfwjeakewfpuf5cahow6cu8opz1a+oxm49zt61wc...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Mon, Aug 15, 2011 at 19:35, Hartmut <[email protected]> wrote:

> data BasicSelect a =  NumberSelect a | StringSelect a
> data Num a => NumberSelect a = SelectionNum a
> data Show a => StringSelect a = SelectionStr a
>

This seems a bit confused.  Additionally, the contexts don't do what you
intend (they're nearly useless and will probably be removed in a future
Haskell revision).

I think what you're reaching for here is a GADT:

> data BasicSelect a where
>   SelectionNum :: Num a => a -> BasicSelect a
>   SelectionStr :: Show a => a -> BasicSelect a

I think you otherwise end up with a typeclass or with existentials; both
introduce complexity, and I think the existentials solution still allows
anything to be stuffed into it, whereas you want to limit it to SelectionNum
or SelectionStr.  (Typeclasses at least require an instance to be defined
first; but they don't prohibit people from doing so, if that's what you're
after.)

-- 
brandon s allbery                                      [email protected]
wandering unix systems administrator (available)     (412) 475-9364 vm/sms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110815/d6500b41/attachment-0001.htm>

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

Message: 4
Date: Tue, 16 Aug 2011 00:15:22 -0400
From: Dean Herington <[email protected]>
Subject: Re: [Haskell-beginners] the first argument of take is an Int
        and not Integral..
To: Sunil S Nandihalli <[email protected]>
Cc: [email protected]
Message-ID: <a06240801ca6f9e957a88@[10.0.1.8]>
Content-Type: text/plain; charset="us-ascii"; Format="flowed"

At 12:28 AM +0530 8/11/11, Sunil S Nandihalli wrote:
>ah that is exactly what I wanted .. thanks Daniel.
>Sunil.
>
>On Thu, Aug 11, 2011 at 12:22 AM, Daniel Fischer 
><<mailto:[email protected]>[email protected]> 
>wrote:
>
>On Wednesday 10 August 2011, 20:42:05, Sunil S Nandihalli wrote:
>>  hmm.. I do agree that taking elements larger than say 2-billion is
>>  insane .. however, I have no intention of doing that. But the thing is
>>  the value I am getting is of type Integer but it actually very small
>>  and I am unable to pass it as is. So is there a way to convert a value
>>  of type Integer to Int ? This might be obvious.. But I am a newbie to
>>  the whole Haskell thing ...
>
>Use fromInteger or fromIntegral
>
>>
>>  Thanks,
>  > Sunil.

But realize that truncation can result:

Prelude> fromInteger 12345678900 :: Int
-539222988

Dean
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20110816/552432c6/attachment-0001.htm>

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

Message: 5
Date: Tue, 16 Aug 2011 09:54:52 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] Question: Data Type for user
        selection
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII

Brandon Allbery <[email protected]> wrote:

> I think what you're reaching for here is a GADT:
>
> > data BasicSelect a where
> >   SelectionNum :: Num a => a -> BasicSelect a
> >   SelectionStr :: Show a => a -> BasicSelect a
>
> I think you otherwise end up with a typeclass or with existentials;
> both introduce complexity, and I think the existentials solution still
> allows anything to be stuffed into it, whereas you want to limit it to
> SelectionNum or SelectionStr.  (Typeclasses at least require an
> instance to be defined first; but they don't prohibit people from
> doing so, if that's what you're after.)

That's a bit of a contradiction, because you are using existentials
yourself in your GADT.


Greets,
Ertugrul


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





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

Message: 6
Date: Tue, 16 Aug 2011 13:37:48 +0530
From: Sunil S Nandihalli <[email protected]>
Subject: [Haskell-beginners] flatten
To: [email protected]
Message-ID:
        <CAP0FD70Ou-tQKmoqxcu=grpym-b2_mkrjjep1gufdkdokpe...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Hello everybody,
 can somebody tell me as to what is wrong with the following code


myflatten::[(a,a)]->[a]
myflatten [] = []
myflatten (fx,sx):xs = fx:sx:(myflatten xs)

It says that there is a parse error on the last line..
Thanks,
Sunil.



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

Message: 7
Date: Tue, 16 Aug 2011 11:09:25 +0300
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] flatten
To: Sunil S Nandihalli <[email protected]>
Cc: [email protected]
Message-ID:
        <caka2jgjjdef-pybvse4e+0+osmynjfpoyq6ejrcojkkfgyp...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

On Tue, Aug 16, 2011 at 11:07 AM, Sunil S Nandihalli
<[email protected]> wrote:
> Hello everybody,
> ?can somebody tell me as to what is wrong with the following code
>
>
> myflatten::[(a,a)]->[a]
> myflatten [] = []
> myflatten (fx,sx):xs = fx:sx:(myflatten xs)
>
> It says that there is a parse error on the last line..
> Thanks,
> Sunil.
>
Hi Sunil,

You need to wrap the pattern in parentheses, ie:

myflatten ((fx,sx):xs) = fx:sx:(myflatten xs)

Michael



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

Message: 8
Date: Tue, 16 Aug 2011 13:39:27 +0530
From: Sunil S Nandihalli <[email protected]>
Subject: Re: [Haskell-beginners] flatten
To: [email protected]
Message-ID:
        <CAP0FD73z-jbk+Qek_X054x7DhzWwDx=J=bz9yvyvtfq6jmp...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

Ah .. I think I realized the problem..
I had to enclose the last pattern with paranthesis .
Thanks,
Sunil.

On Tue, Aug 16, 2011 at 1:37 PM, Sunil S Nandihalli
<[email protected]> wrote:
> Hello everybody,
> ?can somebody tell me as to what is wrong with the following code
>
>
> myflatten::[(a,a)]->[a]
> myflatten [] = []
> myflatten (fx,sx):xs = fx:sx:(myflatten xs)
>
> It says that there is a parse error on the last line..
> Thanks,
> Sunil.
>



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

Message: 9
Date: Tue, 16 Aug 2011 13:41:17 +0530
From: Sunil S Nandihalli <[email protected]>
Subject: Re: [Haskell-beginners] flatten
To: Michael Snoyman <[email protected]>
Cc: [email protected]
Message-ID:
        <CAP0FD72o0B9SqH1yjchkaP9Uhj=1zfzdkl1t9hvc-hygamz...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

thanks Michael
Sunil.

On Tue, Aug 16, 2011 at 1:39 PM, Michael Snoyman <[email protected]> wrote:
> On Tue, Aug 16, 2011 at 11:07 AM, Sunil S Nandihalli
> <[email protected]> wrote:
>> Hello everybody,
>> ?can somebody tell me as to what is wrong with the following code
>>
>>
>> myflatten::[(a,a)]->[a]
>> myflatten [] = []
>> myflatten (fx,sx):xs = fx:sx:(myflatten xs)
>>
>> It says that there is a parse error on the last line..
>> Thanks,
>> Sunil.
>>
> Hi Sunil,
>
> You need to wrap the pattern in parentheses, ie:
>
> myflatten ((fx,sx):xs) = fx:sx:(myflatten xs)
>
> Michael
>



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

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


End of Beginners Digest, Vol 38, Issue 31
*****************************************

Reply via email to