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:  Problems with IO actions (Jason Dusek)
   2.  help with types and composition (Britt Anderson)
   3. Re:  help with types and composition (Sean Bartell)
   4. Fwd: [Haskell-beginners] help with types and composition
      (Alexander Dunlap)
   5. Re:  help with types and composition (Geoffrey Marchant)
   6.  Help with "20 intermediate haskell exercises"
      (Patrick LeBoutillier)
   7. Re:  Help with "20 intermediate haskell exercises" (Brent Yorgey)


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

Message: 1
Date: Thu, 2 Jul 2009 15:31:38 -0700
From: Jason Dusek <[email protected]>
Subject: Re: [Haskell-beginners] Problems with IO actions
To: Adam Bergmark <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

2009/07/01 Adam Bergmark <[email protected]>:
> * I'm still a little unsure of why mapM_, sequence and the
>   like are necessary, could someone please explain this, or
>   point me somewhere where I can read up on it?

  Say we have a list of `IO` actions (type `[IO a]`). We can't
  run lists directly; the runtime runs `IO` action (type `IO
  something`). It's easy to do the translation but we'd rather
  not do it all the time: here's an easy recursive definition of
  `sequence`:

    sequence ops             =  recurse [] ops
     where
      recurse acc [       ]  =  return $ reverse acc
      recurse acc (op:rest)  =  do
        result              <-  op
        sequence' (result:acc) rest

  The other operations are further conveniences that can be
  built up from `sequence`.

> * Why does this problem occur when there is only one action to
>   perform?

  Do you close the handle?

--
Jason Dusek


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

Message: 2
Date: Thu, 02 Jul 2009 13:54:50 -0400
From: Britt Anderson <[email protected]>
Subject: [Haskell-beginners] help with types and composition
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I would like to write a function that takes two lists of numbers and 
returns a number. For simplicity, let's assume an integer, i.e.
myfunc:: [Integer]->[Integer]->Integer
But I can't quite get things right when trying to
define myfunc = ...
 and making the arguments implicit.

My difficulty might be seen with the following two functions,
let,

f1 = (zipWith ($)) . (map  (*))

f2 = sum


if I set the types such that f1 returns [Integer] and sum accepts 
[Integer] it seems that somehow I should be able to compose them in to a 
single function that takes two lists and returns the number, but f1 . f2 
and numerous variations don't work. I can curry some list l and f1 . (f2 
l) works, but is there a way to get f1 and f2 combined into a function 
that accepts two lists?


My problem is not really about writing the function but about 
composition. I can write the function with arguments or a local lambda  
that suits my practical purpose. But the fact that I think I should be 
able to compose f1 . f2 somehow reveals a conceptual misunderstanding 
that I hope someone will help me clear up.

Thank you,
Britt Anderson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090702/5c952c69/attachment-0001.html

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

Message: 3
Date: Fri, 3 Jul 2009 00:07:27 -0400
From: Sean Bartell <[email protected]>
Subject: Re: [Haskell-beginners] help with types and composition
To: Britt Anderson <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

>
> f1 = (zipWith ($)) . (map  (*))
>
> f2 = sum
>

First, f1 would be clearer as zipWith (*).

Second, look at the type of (.):

(.) :: (b -> c) -> (a -> b) -> (a -> c)

Notice that (b -> c) and (a -> b) each take one argument, but f1 takes two
arguments. That's why using (.) with f1 doesn't work.

You can do what you want by uncurrying f1 so it takes only one argument,
which makes it easier to compose. You can then re-curry the result after you
do the composition.

f1 :: [Integer] -> [Integer] -> [Integer]
uncurry f1 :: ([Integer], [Integer]) -> [Integer]
f2 . uncurry f1 :: ([Integer], [Integer]) -> Integer
curry (f2 . uncurry f1) :: [Integer] -> [Integer] -> Integer

I'm sure there are shorter versions that use more than just (.), curry, and
uncurry.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090702/2955a16d/attachment-0001.html

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

Message: 4
Date: Thu, 2 Jul 2009 21:12:29 -0700
From: Alexander Dunlap <[email protected]>
Subject: Fwd: [Haskell-beginners] help with types and composition
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Oops...wrong list address...sorry


---------- Forwarded message ----------
From: Alexander Dunlap <[email protected]>
Date: Thu, Jul 2, 2009 at 8:54 PM
Subject: Re: [Haskell-beginners] help with types and composition
To: Britt Anderson <[email protected]>, [email protected]


On Thu, Jul 2, 2009 at 10:54 AM, Britt Anderson<[email protected]> wrote:
> I would like to write a function that takes two lists of numbers and returns
> a number. For simplicity, let's assume an integer, i.e.
> myfunc:: [Integer]->[Integer]->Integer
> But I can't quite get things right when trying to
> define myfunc = ...
>  and making the arguments implicit.
>
> My difficulty might be seen with the following two functions,
> let,
>
> f1 = (zipWith ($)) . (map  (*))
>
> f2 = sum
>
>
> if I set the types such that f1 returns [Integer] and sum accepts [Integer]
> it seems that somehow I should be able to compose them in to a single
> function that takes two lists and returns the number, but f1 . f2 and
> numerous variations don't work. I can curry some list l and f1 . (f2 l)
> works, but is there a way to get f1 and f2 combined into a function that
> accepts two lists?
>
>
> My problem is not really about writing the function but about composition. I
> can write the function with arguments or a local lambda  that suits my
> practical purpose. But the fact that I think I should be able to compose f1
> . f2 somehow reveals a conceptual misunderstanding that I hope someone will
> help me clear up.
>
> Thank you,
> Britt Anderson
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>

Try

(f2 .) . f1

. The type of (f2 .) is (a -> [Integer]) -> a -> Integer and the type
of (.) is (b -> c) -> (a -> b) -> (a -> c) so for the second (.), b ::
([Integer] -> [Integer]), c :: ([Integer] -> Integer) and a ::
[Integer] so the final type is [Integer] -> [Integer] -> Integer. (It
might be easier to work out the types yourself if you can - my
description is doubtless rather difficult to follow.)

A word of caution, though - most Haskellers in my experience prefer to
not do this type of composition; they find it clearer to specify one
or both of the arguments explicitly.

Hope that helps you,

Alex


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

Message: 5
Date: Fri, 3 Jul 2009 04:00:48 -0600
From: Geoffrey Marchant <[email protected]>
Subject: Re: [Haskell-beginners] help with types and composition
To: Britt Anderson <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

I usually go with
> (f1 .) . f2

Which makes the solution for the particular example:

> sumOfProducts = (sum .) . zipWith (*)

But if you want the operator that achieves this effect for arbitrary
functions,

> ((.) (.) (.))

does the trick. Or, if SKI is your thing, you might also try

> (ap (const ap) (ap (const const) (ap (const ap) const)))

but I don't usually do it that way unless I'm feeling particularly perverse.


On Thu, Jul 2, 2009 at 11:54 AM, Britt Anderson <[email protected]> wrote:

>  I would like to write a function that takes two lists of numbers and
> returns a number. For simplicity, let's assume an integer, i.e.
> myfunc:: [Integer]->[Integer]->Integer
> But I can't quite get things right when trying to
> define myfunc = ...
>  and making the arguments implicit.
>
> My difficulty might be seen with the following two functions,
> let,
>
> f1 = (zipWith ($)) . (map  (*))
>
> f2 = sum
>
>
> if I set the types such that f1 returns [Integer] and sum accepts [Integer]
> it seems that somehow I should be able to compose them in to a single
> function that takes two lists and returns the number, but f1 . f2 and
> numerous variations don't work. I can curry some list l and f1 . (f2 l)
> works, but is there a way to get f1 and f2 combined into a function that
> accepts two lists?
>
>
> My problem is not really about writing the function but about composition.
> I can write the function with arguments or a local lambda  that suits my
> practical purpose. But the fact that I think I should be able to compose f1
> . f2 somehow reveals a conceptual misunderstanding that I hope someone will
> help me clear up.
>
> Thank you,
> Britt Anderson
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090703/1b77074d/attachment-0001.html

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

Message: 6
Date: Fri, 3 Jul 2009 11:58:22 -0400
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Help with "20 intermediate haskell
        exercises"
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi,

I'm running through these Haskell exercises:

  http://dibblego.wordpress.com/2008/09/18/20-intermediate-haskell-exercises/

and I'm stuck at number 9:


class Misty m where
  banana :: (a -> m b) -> m a -> m b
  unicorn :: a -> m a

-- Exercise 9
-- Relative Difficulty: 6
instance Misty ((->) t) where
  banana = ???
  unicorn x = (\t -> x)


I can picture it when m is "[]" or "Maybe", but I can't wrap my head
around the banane implementation for "((->) t)".
I can see that this somewhat looks like a Monad, with unicorn = return
and banana = (flip >>=) or something. Perhaps some kind of reader
Monad?
Can anyone offer any insight?


Patrick

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


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

Message: 7
Date: Fri, 3 Jul 2009 12:08:31 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Help with "20 intermediate haskell
        exercises"
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii

On Fri, Jul 03, 2009 at 11:58:22AM -0400, Patrick LeBoutillier wrote:
> Hi,
> 
> I'm running through these Haskell exercises:
> 
>   http://dibblego.wordpress.com/2008/09/18/20-intermediate-haskell-exercises/
> 
> and I'm stuck at number 9:
> 
> 
> class Misty m where
>   banana :: (a -> m b) -> m a -> m b
>   unicorn :: a -> m a
> 
> -- Exercise 9
> -- Relative Difficulty: 6
> instance Misty ((->) t) where
>   banana = ???
>   unicorn x = (\t -> x)
> 
> 
> I can picture it when m is "[]" or "Maybe", but I can't wrap my head
> around the banane implementation for "((->) t)".
> I can see that this somewhat looks like a Monad, with unicorn = return
> and banana = (flip >>=) or something. Perhaps some kind of reader
> Monad?

Precisely. ((->) t) is the reader monad.

Just follow the types!

  banana :: (a -> m b) -> m a -> m b

substituting ((->) t) for m (remembering that (->) associates to the
right):

  banana :: (a -> t -> b) -> (t -> a) -> t -> b
  banana f g t = ?

Can you figure out how to apply f :: (a -> t -> b) and g :: (t -> a)
to a 't' in order to get a 'b'?

-Brent


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

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


End of Beginners Digest, Vol 13, Issue 2
****************************************

Reply via email to