I'm convinced I've seen a function like this somewhere: [a->b] -> a -> [b] but cannot remember where. Or maybe: Monad m => m (a->b) -> a -> m b ? I could roll my own (*), but I'm trying not to duplicate standard library functions is I can help it. Any pointers? #g -- (*) flist :: [a->b] -> a -> [b] flist fs a = map (flip ($) a) fs flist [(1*),(2*),(3*)] 5 -- = [5,10,15] ------------------- Graham Klyne <GK@NineByNine.org> PGP: 0FAA 69FF C083 000B A2E9 A131 01B9 1C7A DBCA CB5E
On Tue, 17 Jun 2003 21:01:57 +0100 Graham Klyne <gk@ninebynine.org> wrote:
I'm convinced I've seen a function like this somewhere: [a->b] -> a -> [b] but cannot remember where. Or maybe: Monad m => m (a->b) -> a -> m b ?
I could roll my own (*), but I'm trying not to duplicate standard library functions is I can help it. Any pointers?
The closest function I see is ap :: Monad m => m (a -> b) -> m a -> m b (so you could write your function as f fs a = ap fs (return a) not that I would recommend it). Also you may want to check out the Haskell reference at zvon.org, it's indexed by type as well.
#g --
(*)
flist :: [a->b] -> a -> [b] flist fs a = map (flip ($) a) fs or much nicer (IMO) flist fs a = map ($ a) fs or breakin' out the point-free style, flist = flip (map . flip ($)) -- okay, so I wouldn't recommend this
the generalized solution being simply, f mf x = do f <- mf return (f x) ap is almost certainly liftM2 ($) or equivalently, ap mf mx = do f <- mf x <- mx return (f x)
flist [(1*),(2*),(3*)] 5 -- = [5,10,15]
At 20:15 17/06/03 -0400, Derek Elkins wrote:
On Tue, 17 Jun 2003 21:01:57 +0100 Graham Klyne <gk@ninebynine.org> wrote:
I'm convinced I've seen a function like this somewhere: [a->b] -> a -> [b] but cannot remember where. Or maybe: Monad m => m (a->b) -> a -> m b ?
I could roll my own (*), but I'm trying not to duplicate standard library functions is I can help it. Any pointers?
The closest function I see is ap :: Monad m => m (a -> b) -> m a -> m b
That's the one I was trying to remember! Thanks.
Also you may want to check out the Haskell reference at zvon.org, it's indexed by type as well.
Ah! Very useful. Thanks again.
flist :: [a->b] -> a -> [b] flist fs a = map (flip ($) a) fs
or much nicer (IMO) flist fs a = map ($ a) fs
Ah, yes, I'd not quite grasped that sections can be used "either way" round like this. I agree that's much neater, and this way of combining $ and map an idiom that I think could be used in other ways.
or breakin' out the point-free style, flist = flip (map . flip ($)) -- okay, so I wouldn't recommend this
I keep on reading about this "point free style", but can't find any discussion of it. Are there any pointers (sic) ? #g ------------------- Graham Klyne <GK@NineByNine.org> PGP: 0FAA 69FF C083 000B A2E9 A131 01B9 1C7A DBCA CB5E
or breakin' out the point-free style, flist = flip (map . flip ($)) -- okay, so I wouldn't recommend this
I keep on reading about this "point free style", but can't find any discussion of it. Are there any pointers (sic) ?
Search for "Squiggol" or "Bird-Meertens Formalism". A group of functional programmers at Oxford. (Richard Bird and Lambert Meertens are the ones the formalism was named after). I see some citations of Backus, J. 1978. "Can Programming Be Liberated from the von Neumann Style? A Functional Style and Its Algebra of Programs," Communications of the Association for Computing Machinery 21:613-641. which appears to be available (as a scan) at, amongst others, http://www.stanford.edu/class/cs242/readings/backus.pdf Another source is http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/index.html#... (Jeremy Gibbons introduced me to the concept when I was an undergraduate). This is an introduction to Squiggol: http://web.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/index.html#... This style underlies a lot of expert Haskeller's intuitions. The mass of cute symbols can drive you crazy, though... see Erik Meijer et al.'s paper _Functional Programming with Bananas, Lenses, and Barbed Wire_: http://www.cse.ogi.edu/~erik/Personal/classic.htm Hope this helps. (going on the Wiki as PointFreeStyle, linked from CommonHaskellIdioms). --KW 8-)
On 2003-06-17 at 20:15EDT Derek Elkins wrote:
The closest function I see is ap :: Monad m => m (a -> b) -> m a -> m b (so you could write your function as f fs a = ap fs (return a) not that I would recommend it). Also you may want to check out the Haskell reference at zvon.org, it's indexed by type as well.
That's useful. It reminds me to ask whether anyone is working on adding a search by type facility to any of the haskell systems. I remember a talk by folk from York about how they did it (years ago), and being impressed, so it seems a shame that we don't have this now. Jón -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk
Derek Elkins wrote:
flist :: [a->b] -> a -> [b] flist fs a = map (flip ($) a) fs
or much nicer (IMO) flist fs a = map ($ a) fs
This is a case where I'd prefer a list comprehension: flist fs a = [ f a | f <- fs ] (and this could be a monad comprehension, if Haskell still had them...)
the generalized solution being simply, f mf x = do f <- mf return (f x)
Or just replace map by fmap in your flist from above. All the best Christian Sievers
On Thu, 19 Jun 2003 18:05:11 +0200 Christian Sievers <sievers@math2.nat.tu-bs.de> wrote:
Derek Elkins wrote:
flist :: [a->b] -> a -> [b] flist fs a = map (flip ($) a) fs
or much nicer (IMO) flist fs a = map ($ a) fs
This is a case where I'd prefer a list comprehension:
flist fs a = [ f a | f <- fs ]
(and this could be a monad comprehension, if Haskell still had them...)
I don't think Haskell ever had them (I'd have to check). Gofer did. Anyways, do-notation is about as expressive as monad comprehensions. With do-notation you have to add guards explicitly, but you don't have to end with a return and you don't need to bind variables, e.g. I believe [() | _ <- putStrLn "foo"] would be necessary (well, not in this case, but the general one).
the generalized solution being simply, f mf x = do f <- mf return (f x)
Or just replace map by fmap in your flist from above.
That generalizes it, but in a different way since (in Haskell) Monad isn't a subclass of Functor...
Derek Elkins wrote:
Christian Sievers <sievers@math2.nat.tu-bs.de> wrote:
(and this could be a monad comprehension, if Haskell still had them...)
I don't think Haskell ever had them (I'd have to check). Gofer did.
They were put in for Haskell 1.4, and removed again for Haskell 98 because of the horrendously confusing error messages they caused. http://www.haskell.org/definition/ --KW 8-) -- Keith Wansbrough <kw217@cl.cam.ac.uk> http://www.cl.cam.ac.uk/users/kw217/ University of Cambridge Computer Laboratory.
Christian Sievers wrote:
This is a case where I'd prefer a list comprehension:
flist fs a = [ f a | f <- fs ]
(and this could be a monad comprehension, if Haskell still had them...)
And it still has them, you just have to get accustomed to the slightly different syntax :-) flist fs a = do f <- fs; return (f a) Wolfgang
participants (7)
-
Christian Sievers -
Derek Elkins -
Graham Klyne -
Graham Klyne -
Jon Fairbairn -
Keith Wansbrough -
Wolfgang Lux