Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Restoring interleaved lists? (Patrick LeBoutillier)
   2.  Argument order and partial function application (Tom Murphy)
   3. Re:  Argument order and partial function  application
      (Brandon S Allbery KF8NH)
   4. Re:  Argument order and partial function  application
      (Lyndon Maydwell)
   5. Re:  Argument order and partial function  application (Tom Murphy)
   6.  Data Class problem (Michael Bradley)
   7. Re:  Data Class problem (Ozgur Akgun)


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

Message: 1
Date: Thu, 19 Aug 2010 18:23:47 -0400
From: Patrick LeBoutillier <patrick.leboutill...@gmail.com>
Subject: Re: [Haskell-beginners] Restoring interleaved lists?
To: Felipe Lessa <felipe.le...@gmail.com>
Cc: beginners <beginners@haskell.org>
Message-ID:
        <aanlktinjbgfvysli3ewgro=hoh8xfg5qmvm-hr=tj...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

Felipe,

On Thu, Aug 12, 2010 at 9:23 PM, Felipe Lessa <felipe.le...@gmail.com> wrote:
> On Thu, Aug 12, 2010 at 10:11 PM, Patrick LeBoutillier
> <patrick.leboutill...@gmail.com> wrote:
>> I need a function that, given t and the list of t*m measures, can
>> spilt the measures by track, returning a list of t lists, each
>> containing m measures. I cannot figure out how to do this, even though
>> it seems to me like it shouldn't be too hard... I can't figure out how
>> to "update" the lists of lists when I want to add a new element.
>>
>> Do anyone have any ideas?
>
> I won't post the complete code and will just throw ideas around,
> please let us know if you still get stuck =).

Perfect!

>
> We have the following function in the Prelude:
>
>  splitAt :: Int -> [a] -> ([a], [a])
>
> For example,
>
>  Prelude> splitAt 3 [1..12]
>  ([1,2,3],[4,5,6,7,8,9,10,11,12])
>
> So you can write the following function (you can choose another name):
>
>  separate :: Int -> [a] -> [[a]]
>
> For example,
>
>  Prelude> separate 3 [1..12]
>  [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
>
> But that's not what we wanted.  Hmmm, from Data.List we have
>
>  transpose :: [[a]] -> [[a]]
>
> For example:
>
>  Prelude Data.List> transpose [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
>  [[1,4,7,10],[2,5,8,11],[3,6,9,12]]
>
> Bingo!
>
> Hope that helps ;-),

Yes, that's exactly what I was looking for!

Thanks,

Patrick
>
> --
> Felipe.
>



-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 2
Date: Thu, 19 Aug 2010 18:53:08 -0400
From: Tom Murphy <amin...@gmail.com>
Subject: [Haskell-beginners] Argument order and partial function
        application
To: beginners@haskell.org
Message-ID:
        <aanlktin5oxdjgutbvc3ygm-jqvqkjdjgob-afx345...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi all,
     Thanks for this great resource. I've got a basic question about partial
function application:


Let's say I have a very basic function:

     volumeFunction length width height = length * width * height

Its type signature is:

     volumeFunction :: Int -> Int -> Int -> Int



This implies that I first pass it the length, *then* the width, *then* the
height.
So while it is very easy for me to write

     volumeWithLengthTen = volumeFunction 10

, it's not possible to create a volumeWithWidthTen or volumeWithHeightTen.

     I can see how I might be able to "hack" my way into accessing the
"width" argument with infix functions, but there's no way at all that I can
see to access the height. This seems arbitrary to me: my volumeFunction
doesn't need them in the order they're given. Is there a way around this?
Could I somehow combine three smaller functions?


I appreciate any insight.

Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100819/b89d0046/attachment-0001.html

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

Message: 3
Date: Thu, 19 Aug 2010 19:13:56 -0400
From: Brandon S Allbery KF8NH <allb...@ece.cmu.edu>
Subject: Re: [Haskell-beginners] Argument order and partial function
        application
To: beginners@haskell.org
Message-ID: <4c6dbab4.8090...@ece.cmu.edu>
Content-Type: text/plain; charset=UTF-8

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 8/19/10 18:53 , Tom Murphy wrote:
>      I can see how I might be able to "hack" my way into accessing the
> "width" argument with infix functions, but there's no way at all that I can
> see to access the height. This seems arbitrary to me: my volumeFunction
> doesn't need them in the order they're given. Is there a way around this?
> Could I somehow combine three smaller functions?

volumeGivenLength, volumeGivenWeight, volumeGivenHeight: Int -> (Int -> Int
- -> Int)
- -- note that the above signature is the same as Int -> Int -> Int -> Int
volumeGivenLength = volumeFunction -- function taking w, h
volumeGivenWidth = flip volumeFunction -- function taking l, h
volumeGivenHeight = flip (flip . volumeFunction) -- function taking l, w

You can in general achieve any desired permutation of parameters, at the
expense of your sanity:

    <allbery_b> @pl \h -> \a b c d e f g -> foo a b c d e f g h
    <lambdabot> flip (flip . ((flip . ((flip . ((flip . ((flip . (flip .))
.)) .)) .)) .) . foo

- -- 
brandon s. allbery     [linux,solaris,freebsd,perl]      allb...@kf8nh.com
system administrator  [openafs,heimdal,too many hats]  allb...@ece.cmu.edu
electrical and computer engineering, carnegie mellon university      KF8NH
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkxturQACgkQIn7hlCsL25U/VACeKuMkrF9FkHnFmwPOGg8jEnCR
LsAAoKp7KX6e2CIuSPa0sORbtst3bXt+
=mNM2
-----END PGP SIGNATURE-----


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

Message: 4
Date: Fri, 20 Aug 2010 07:26:42 +0800
From: Lyndon Maydwell <maydw...@gmail.com>
Subject: Re: [Haskell-beginners] Argument order and partial function
        application
To: Tom Murphy <amin...@gmail.com>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimmyhujnnutc5mevonbtjsn+q+vrfd90zvpn...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

I'm guessing that this isn't the actual function, as your volume
function is already agnostic regarding argument order. Making

> volumeWithWidthTen = volumeFunction 10

If the argument order does matter you can get around some order
restrictions using functions like flip, but it's often clearer to be
explicit:

> volumeWithWidthTen length = volumeFunction length 10


Is that what you were looking for?

On Fri, Aug 20, 2010 at 6:53 AM, Tom Murphy <amin...@gmail.com> wrote:
> Hi all,
>      Thanks for this great resource. I've got a basic question about 
> partial
> function application:
>
> Let's say I have a very basic function:
>      volumeFunction length width height = length * width * height
> Its type signature is:
>      volumeFunction :: Int -> Int -> Int -> Int
>
>
> This implies that I first pass it the length, *then* the width, *then* the
> height.
> So while it is very easy for me to write
>      volumeWithLengthTen = volumeFunction 10
> , it's not possible to create a volumeWithWidthTen or volumeWithHeightTen.
>      I can see how I might be able to "hack" my way into accessing the
> "width" argument with infix functions, but there's no way at all that I can
> see to access the height. This seems arbitrary to me: my volumeFunction
> doesn't need them in the order they're given. Is there a way around this?
> Could I somehow combine three smaller functions?
>
> I appreciate any insight.
> Tom
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>


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

Message: 5
Date: Thu, 19 Aug 2010 19:39:31 -0400
From: Tom Murphy <amin...@gmail.com>
Subject: Re: [Haskell-beginners] Argument order and partial function
        application
To: beginners@haskell.org
Message-ID:
        <aanlkti=dsg0k2pm-sebvau24rcevnquodprxlxvon...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

>
>
> If the argument order does matter you can get around some order
> restrictions using functions like flip, but it's often clearer to be
> explicit:
>
> > volumeWithWidthTen length = volumeFunction length 10
>
>
> Is that what you were looking for?
>
>

It's exactly what I'm looking for; thank you.

A nice flash of insight once in a while keeps the brain sharp.



Thanks too, Brandon, for the education about flips.

Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100819/e9464d79/attachment-0001.html

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

Message: 6
Date: Fri, 20 Aug 2010 08:58:07 +0000
From: Michael Bradley <michael.brad...@hotmail.co.uk>
Subject: [Haskell-beginners] Data Class problem
To: <beginners@haskell.org>
Message-ID: <blu129-w29f9b6fb20a0e06cc415b4be...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"


Hello all!

I have been experimenting with data and class declarations and came across a 
problem. I have been trying to do this:




        Code:
        data Fire = Burn | Ember
data Water = Bubble | WaterGun

class Elements a

instance Elements Fire
instance Elements Water

data Elemental = Elemental { name :: String,
                             move :: (Elements a) => a
                           }
So, the idea was that "move" in the data constructor "Elemental" 
would be able to take any value from either the type Fire or Water. 
However, the error message tells me this is an illegal polymorphic type.



Therefore, I tried creating a function that could read my value for me 
after "show" was applied to the move. Hence, the data declaration for 
Elemental could now assign "move" to a String. The function looked like 
this:




        Code:
        getMove :: (Elements b) => String -> b
getMove x = read x :: (Elements a) => a
This will not work either, as the function "read" complains of 
ambiguity in the letter a. I also tried this (amongst other attempts)




        Code:
        getMove :: (Elements b) => String -> b
getMove "Burn" = Burn
getMove "Ember" = Ember
getMove "Bubble" = Bubble
getMove "WaterGun" = WaterGun
getMove _ = error "Unknown move!"
The above caused the function to infer the type Fire, and then 
complain about the type Water. So, how can I either create a function 
that can return multiple types like I am trying to above, or is there a 
way to adjust the data declaration for Elemental?



Also, I have noticed that 3 :: (Num a) => a will work but Burn :: (Elements a) 
=> a causes an ambiguity error. Why is this the case? 



Please help!

Mike


                                          
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100820/4f26ad18/attachment-0001.html

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

Message: 7
Date: Fri, 20 Aug 2010 11:26:37 +0100
From: Ozgur Akgun <ozgurak...@gmail.com>
Subject: Re: [Haskell-beginners] Data Class problem
To: Michael Bradley <michael.brad...@hotmail.co.uk>
Cc: beginners@haskell.org
Message-ID:
        <aanlktimu=9bmjkj4fywmzmrdyvgkjrw-4eeizgcdw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I am sure others will give a better reply, but using RankNTypes you can
type-check the first alternative you mentioned. Depending on what you are
planning to do with the move field, it should be OK to use RankNTypes here,
I guess.

HTH,
Ozgur

On 20 August 2010 09:58, Michael Bradley <michael.brad...@hotmail.co.uk>wrote:

>  Hello all!
>
> I have been experimenting with data and class declarations and came across
> a problem. I have been trying to do this:
>
>  Code:
>
> data Fire = Burn | Ember
> data Water = Bubble | WaterGun
>
> class Elements a
>
> instance Elements Fire
> instance Elements Water
>
> data Elemental = Elemental { name :: String,
>                              move :: (Elements a) => a
>                            }
>
> So, the idea was that "move" in the data constructor "Elemental" would be
> able to take any value from either the type Fire or Water. However, the
> error message tells me this is an illegal polymorphic type.
>
> Therefore, I tried creating a function that could read my value for me
> after "show" was applied to the move. Hence, the data declaration for
> Elemental could now assign "move" to a String. The function looked like
> this:
>
>  Code:
>
> getMove :: (Elements b) => String -> b
> getMove x = read x :: (Elements a) => a
>
> This will not work either, as the function "read" complains of ambiguity in
> the letter a. I also tried this (amongst other attempts)
>
>  Code:
>
> getMove :: (Elements b) => String -> b
> getMove "Burn" = Burn
> getMove "Ember" = Ember
> getMove "Bubble" = Bubble
> getMove "WaterGun" = WaterGun
> getMove _ = error "Unknown move!"
>
> The above caused the function to infer the type Fire, and then complain
> about the type Water. So, how can I either create a function that can return
> multiple types like I am trying to above, or is there a way to adjust the
> data declaration for Elemental?
>
> Also, I have noticed that 3 :: (Num a) => a will work but Burn ::
> (Elements a) => a causes an ambiguity error. Why is this the case?
>
> Please help!
>
> Mike
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100820/94393933/attachment.html

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 26, Issue 40
*****************************************

Reply via email to