
Hello, as I am newbie to Haskell and my introductory question is: given functions say f and g with type signatures f :: (Num a) => [a] -> [a] -> [(a,a)] // f takes two lists and zips them into one in some special way g :: (Num a) => a -> [(a,a)] -> [a] // g using some Num value calculates list of singletons from list of pairs of course g 0 :: (Num a) => [(a,a)] ->[a] now I want to create function h :: (Num a) => [a] -> [a] -> [a] in such way that (g 0) consumes output of f. But when I try Prelude> :t (g 0).f I get an error: <interactive>:1:9: Couldn't match expected type `[(a0, a0)]' with actual type `[a1] -> [(a1, a1)]' Expected type: [a1] -> [(a0, a0)] Actual type: [a1] -> [a1] -> [(a1, a1)] In the second argument of `(.)', namely `f' In the expression: (g 0) . f In pointfull representation it works well Prelude> let h x y = (g 0 (f x y)) How to do pointfree definition of h? Ajschylos.

Prelude> let h x y = (g 0 (f x y))
How to do pointfree definition of h?
See the thread http://thread.gmane.org/gmane.comp.lang.haskell.cafe/70488for related material. Regards, Sean

On Monday 11 April 2011 11:22:51, Adam Krauze wrote:
Hello, as I am newbie to Haskell and my introductory question is:
given functions say f and g with type signatures
f :: (Num a) => [a] -> [a] -> [(a,a)] // f takes two lists and zips them into one in some special way g :: (Num a) => a -> [(a,a)] -> [a] // g using some Num value calculates list of singletons from list of pairs
of course g 0 :: (Num a) => [(a,a)] ->[a]
now I want to create function h :: (Num a) => [a] -> [a] -> [a] in such way
that (g 0) consumes output of f.
But when I try
Prelude> :t (g 0).f
I get an error:
<interactive>:1:9: Couldn't match expected type `[(a0, a0)]' with actual type `[a1] -> [(a1, a1)]' Expected type: [a1] -> [(a0, a0)] Actual type: [a1] -> [a1] -> [(a1, a1)] In the second argument of `(.)', namely `f' In the expression: (g 0) . f
In pointfull representation it works well
Prelude> let h x y = (g 0 (f x y))
How to do pointfree definition of h?
Composition treats one argument, so h x y = g 0 (f x y) = (g 0) (f x y) ~> h x = (g 0) . (f x) ~> h = ((g 0) .) . f for each argument of the function to be applied first, you need one composition operator. But (((foo .) .) .) . bar and such quickly become unreadable, so know when to stop point-freeing such multi-compositions.

On Mon, 2011-04-11 at 11:22 +0200, Adam Krauze wrote:
f :: (Num a) => [a] -> [a] -> [(a,a)] // f takes two lists and zips them into one in some special way g :: (Num a) => a -> [(a,a)] -> [a] // g using some Num value calculates list of singletons from list of pairs
Prelude> let h x y = (g 0 (f x y))
How to do pointfree definition of h?
You can eliminate the second point, y, pretty easily by just using function composition: let h x = g 0 . f x To eliminate x, we can first rewrite this expression using a section of the (.) operator. let h x = (g 0 .) (f x) and then introduce another function composition: let h = (g 0 .) . f Whether that's clearer than the pointed definition is up for debate, but there it is. Just keep in mind that sections of (.) are very confusing to some people. -- Chris Smith

In addition to what others have said, you could use pointfree[1] to do this automagically!
pointfree "h x y = (g 0 (f x y))" h = (g 0 .) . f
[1] http://hackage.haskell.org/package/pointfree
On 11 April 2011 10:22, Adam Krauze
Hello, as I am newbie to Haskell and my introductory question is:
given functions say f and g with type signatures
f :: (Num a) => [a] -> [a] -> [(a,a)] // f takes two lists and zips them into one in some special way g :: (Num a) => a -> [(a,a)] -> [a] // g using some Num value calculates list of singletons from list of pairs
of course g 0 :: (Num a) => [(a,a)] ->[a]
now I want to create function h :: (Num a) => [a] -> [a] -> [a] in such way
that (g 0) consumes output of f.
But when I try
Prelude> :t (g 0).f
I get an error:
<interactive>:1:9: Couldn't match expected type `[(a0, a0)]' with actual type `[a1] -> [(a1, a1)]' Expected type: [a1] -> [(a0, a0)] Actual type: [a1] -> [a1] -> [(a1, a1)] In the second argument of `(.)', namely `f' In the expression: (g 0) . f
In pointfull representation it works well
Prelude> let h x y = (g 0 (f x y))
How to do pointfree definition of h?
Ajschylos.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ozgur Akgun

Adam Krauze schrieb:
Hello, as I am newbie to Haskell and my introductory question is:
given functions say f and g with type signatures
f :: (Num a) => [a] -> [a] -> [(a,a)] // f takes two lists and zips them into one in some special way g :: (Num a) => a -> [(a,a)] -> [a] // g using some Num value calculates list of singletons from list of pairs
of course g 0 :: (Num a) => [(a,a)] ->[a]
now I want to create function h :: (Num a) => [a] -> [a] -> [a] in such way
that (g 0) consumes output of f.
But when I try
Prelude> :t (g 0).f
http://www.haskell.org/haskellwiki/Composing_functions_with_multiple_values
participants (6)
-
Adam Krauze
-
Chris Smith
-
Daniel Fischer
-
Henning Thielemann
-
Ozgur Akgun
-
Sean Leather