pointfree

Point-free lens creation does not type check

筅森魡賤 提交于 2019-12-07 04:07:02
问题 In the function test , I traverse over a list, generate lenses from it's members, and then print some data. This works when I use a pointful call style. It fails to typecheck when I make it point-free. Why is this the case, and how can I solve this problem? It looks like to me that GHC is not retaining the information that the higher-ranked f (in the lens) is a Functor when using point-free style, but I'm not too sure. I'm using GHC 7.8.3 {-# LANGUAGE RankNTypes #-} {-# LANGUAGE

Number of arguments and point-free in Haskell [duplicate]

爱⌒轻易说出口 提交于 2019-12-06 17:30:31
问题 This question already has answers here : Defining a function by equations with different number of arguments (3 answers) Closed 2 years ago . With multiple pattern-matching, different numbers of arguments are impossible, even with point-free! foo True b = b + 2 foo _ = id doesn't work for example. But foo True = (+2) foo _ = id does. Sometimes we can use point-free only in one part of the function, so... Why? Is it too hard for GHC? :'( 回答1: Why? Is it too hard for GHC? No . It is not at all

Haskell: mapping function application

≯℡__Kan透↙ 提交于 2019-12-05 18:30:44
Part of some computation I am doing in Haskell results in a list of functions that map Float to Float . I'd like to apply a single argument to all these functions, like so: -- x :: Float -- functions :: [Float -> Float] map (\f -> f x) functions Is there a way to do this without making use of a throw-away lambda function? I've searched Hoogle for what I think the signature should be ( [a -> b] -> a -> [b] ) with no luck. You can use the $ operator, which is just function application: map ($ x) functions (This presupposes that x is in scope for the expression.) Hoogle can only find functions,

Write f in pointfree-style?

穿精又带淫゛_ 提交于 2019-12-05 16:05:03
Say I have functions g :: a -> b, h :: a -> c and f :: b -> c -> d. Is it possible to write the function f' :: a -> a -> d given by f' x y = f (g x) (h y) in point free style?. One can write the function f' a -> d, f' x = f (g x) (h x) in point free style by setting f' = (f <$> g) <*> h but I couldn't figure out how to do the more general case. Lynn We have: k x y = (f (g x)) (h y) and we wish to write k in point-free style. The first argument passed to k is x . What do we need to do with x ? Well, first we need to call g on it, and then f , and then do something fancy to apply this to (h y) .

Is there a point-free way to convert a conditional check into a Maybe type of the input?

本小妞迷上赌 提交于 2019-12-05 08:13:07
I am just working through some simple exercises in haskell and was wondering if there was a point-free way of converting an if-then-else statement into a Maybe type: Nothing being returned if the condition is false, and Just the input if the condition is true. In short, given some: maybeIf :: (a -> Bool) -> a -> Maybe a maybeIf cond a = if cond a then Just a else Nothing Is there an implementation that is point-free with respect to a ? I've also been looking at a more concrete version, a -> Maybe a , and feel like there may be an answer somewhere in Control.Arrow . However, since Maybe is a

Confusion about currying and point free style in Haskell

不想你离开。 提交于 2019-12-05 02:48:27
I was trying to implement the function every :: (a -> IO Bool) -> [a] -> IO Bool which was the topic for this question . I tried to do this without explicit recursion . I came up with the following code every f xs = liftM (all id) $ sequence $ map f xs My function didn't work since it wasn't lazy (which was required in the question), so no upvotes there :-). However, I did not stop there. I tried to make the function point-free so that it would be shorter (and perhaps even cooler). Since the arguments f and xs are the last ones in the expression I just dropped them: every = liftM (all id) $

What is the derivation that shows Haskell's \\x -> (x, x) equivalent to join (,)?

邮差的信 提交于 2019-12-04 23:56:37
According to pointfree : \x -> (x, x) is equivalent to: join (,) What is the derivation that shows this? Look at the type signatures: \x -> (x, x) :: a -> (a, a) (,) :: a -> b -> (a, b) join :: Monad m => m (m a) -> m a It should be noted that ((->) r) is an instance of the Monad typeclass. Hence, on specializing: join :: (r -> r -> a) -> (r -> a) What join does for functions is apply the given function twice to the same argument: join f x = f x x -- or join f = \x -> f x x From this, we can see trivially: join (,) = \x -> (,) x x -- or join (,) = \x -> (x, x) Qed. I like Aadits intuitive

Tacit function composition in Haskell

柔情痞子 提交于 2019-12-04 23:53:21
Say I have a mean function defined like so: mean xs = sum xs / (fromIntegral $ length xs) but I want it in some tacit form, like this: mean = sum / (fromIntegral . length) Is there a built-in Haskell way to do something along these lines without having to build up my own tacit function (something like this): tacit :: (a -> b -> c) -> (d -> a) -> (d -> b) -> d -> c tacit a b c i = a (b i) (c i) In this form, the function looks like this: mean = tacit (/) sum (fromIntegral . length) but it feels like there might be a way to avoid having to use an explicit function such as this. I'm just

Number of arguments and point-free in Haskell [duplicate]

一曲冷凌霜 提交于 2019-12-04 23:15:11
This question already has answers here : Defining a function by equations with different number of arguments (3 answers) Closed 2 years ago . With multiple pattern-matching, different numbers of arguments are impossible, even with point-free! foo True b = b + 2 foo _ = id doesn't work for example. But foo True = (+2) foo _ = id does. Sometimes we can use point-free only in one part of the function, so... Why? Is it too hard for GHC? :'( Why? Is it too hard for GHC? No . It is not at all too hard for GHC. Actually, this is the fault of the Haskell Report. See: Haskell Report 2010 > Declarations

When is it appropriate to choose point-free style vs a data-centric style in functional programming?

最后都变了- 提交于 2019-12-04 04:43:20
问题 In case it matters this is about functional programming in JavaScript and in my examples I’ll be using Ramda. While everybody at work has fully embraced functional programming, there’s also a lot of discussions around how to do it “right”. These two functions will do exactly the same thing: take a list and return a new list in which all strings have been trimmed. // data-centric style const trimList = list => R.map(R.trim, list); // point-free style const trimList = R.map(R.trim); So far so