-1

I need any function with this signature :

ignore :: Applicative f => f a -> f ()

Can someone point me to the right direction ?

Thanks!

Tomer
  • 1,159
  • 7
  • 15
  • 2
    Did you try [hoogling it](https://hoogle.haskell.org/?hoogle=Applicative+f+%3D%3E+f+a+-%3E+f+%28%29&scope=set%3Aincluded-with-ghc)? `void` has exactly that signature... – Amadan Mar 10 '20 at 01:02
  • 3
    Applicatives are functors too. – Will Ness Mar 10 '20 at 01:40
  • ignore a = fmap (\x -> ()) a --- thanks! – Tomer Mar 10 '20 at 01:43
  • `const` can be nice but I prefer [`-XBlockArguments`](https://ghc.gitlab.haskell.org/ghc/doc/users_guide/exts/block_arguments.html?highlight=blockarguments#extension-BlockArguments): `void = fmap \_ -> ()` – Iceland_jack Mar 31 '20 at 19:46

2 Answers2

3

Like @Amadan said in the comments, you can easily find answers to this kind of questions by using the Hoogle search engine. Nevertheless, here are a few ways you can do this:

  1. You can observe that what you need is a mapping to a constant value (()). In Haskell, that's const (). All you have to do now is modify this function so that it will affect the value inside an (applicative) functor. And what value makes a function into a function on functors? fmap. Put all of this together, and you get solution #1: fmap $ const ().

  2. There's a function in the Prelude that takes a functor and replaces its value with another: (<$) :: Functor f => a -> f b -> f a. Here, a is (), so you get solution #2: () <$.

  3. Use Hoogle, and you get void from Data.Functor and Control.Monad. This function is also useful when you have a monadic action and want to ignore its result by switching it out with ().

  4. Write it out the long-winded way with a lambda (as you did in the comments): ignore f = (\_ -> ()) <$> f, or just ignore = fmap $ \_ -> ()

MikaelF
  • 3,518
  • 4
  • 20
  • 33
1

How about turning f a into f b for an Applicative f, do you know of anything that can help you with that?

Something that changes the value "on the inside" while keeping the "outside" intact?

Hint: Applicatives are Functors too.

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • Uhh, I don't think `Applicative f => f a -> f b` is inhabited (unless you include bottoms, etc). If it were (call it `foo`), then you'd have `unsafeCoerce = runIdentity . foo . pure`. – Joseph Sible-Reinstate Monica Mar 10 '20 at 01:27
  • 1
    I think @WillNess's point was that `Functor f => (a -> b) -> f a -> f b` is inhabited. – K. A. Buhr Mar 10 '20 at 17:11
  • 1
    @K.A.Buhr yes exactly. I initially wrote `Applicative f => f a -> f b` intending it kind of informally, but taken literally it made little sense. – Will Ness Mar 10 '20 at 20:59
  • 1
    Oh, I guess I should have looked at the edit history. I though @JosephSible was commenting on the current edit and thought he was reading too much into it. The comments make sense now. – K. A. Buhr Mar 10 '20 at 21:30