pointfree

Understanding `ap` in a point-free function in Haskell

被刻印的时光 ゝ 提交于 2019-12-03 16:01:22
问题 I am able to understand the basics of point-free functions in Haskell: addOne x = 1 + x As we see x on both sides of the equation, we simplify it: addOne = (+ 1) Incredibly it turns out that functions where the same argument is used twice in different parts can be written point-free! Let me take as a basic example the average function written as: average xs = realToFrac (sum xs) / genericLength xs It may seem impossible to simplify xs , but http://pointfree.io/ comes out with: average = ap ((

Applying multiple functions to the same value point-free style in Haskell

試著忘記壹切 提交于 2019-12-03 05:38:18
问题 I was bored one day and wanted to exercise my brain, so I decided to do the 99 Haskell Problems but restricted myself to doing them in point-free style. A problem that seems to crop up a lot when I'm doing things in point-free style is this: How do you apply multiple functions to the same value while keeping each result as an independent entity? Using pointed notation: foobar x = [id x, reverse x] And what I've come up with so far in point-free notation: foobar' = `map` [id, reverse] ($ x) I

Understanding `ap` in a point-free function in Haskell

丶灬走出姿态 提交于 2019-12-03 05:18:32
I am able to understand the basics of point-free functions in Haskell: addOne x = 1 + x As we see x on both sides of the equation, we simplify it: addOne = (+ 1) Incredibly it turns out that functions where the same argument is used twice in different parts can be written point-free! Let me take as a basic example the average function written as: average xs = realToFrac (sum xs) / genericLength xs It may seem impossible to simplify xs , but http://pointfree.io/ comes out with: average = ap ((/) . realToFrac . sum) genericLength That works. As far as I understand this states that average is the

In Haskell performing `and` and `or` for boolean functions

谁说我不能喝 提交于 2019-12-03 01:02:26
问题 I just wrote the following two functions: fand :: (a -> Bool) -> (a -> Bool) -> a -> Bool fand f1 f2 x = (f1 x) && (f2 x) f_or :: (a -> Bool) -> (a -> Bool) -> a -> Bool f_or f1 f2 x = (f1 x) || (f2 x) They might be used to combined the values of two boolean functions such as: import Text.ParserCombinators.Parsec import Data.Char nameChar = satisfy (isLetter `f_or` isDigit) After looking at these two functions, I came to the realization that they are very useful. so much so that I now suspect

In Haskell performing `and` and `or` for boolean functions

两盒软妹~` 提交于 2019-12-02 14:22:56
I just wrote the following two functions: fand :: (a -> Bool) -> (a -> Bool) -> a -> Bool fand f1 f2 x = (f1 x) && (f2 x) f_or :: (a -> Bool) -> (a -> Bool) -> a -> Bool f_or f1 f2 x = (f1 x) || (f2 x) They might be used to combined the values of two boolean functions such as: import Text.ParserCombinators.Parsec import Data.Char nameChar = satisfy (isLetter `f_or` isDigit) After looking at these two functions, I came to the realization that they are very useful. so much so that I now suspect that they are either included in the standard library, or more likely that there is a clean way to do

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

女生的网名这么多〃 提交于 2019-12-01 22:19:48
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 good. However with a more complex example, the difference between the two styles is striking: take a

What is the equivalent to (+1) for the subtraction, since (-1) is seen as a negative number? [duplicate]

感情迁移 提交于 2019-12-01 15:38:49
Possible Duplicate: Currying subtraction I started my first haskell project that is not from a tutorial, and of course I stumble on the simplest things. I have the following code: moveUp y = modifyMVar_ y $ return . (+1) moveDn y = modifyMVar_ y $ return . (-1) It took me some time to understand why my code wouldn't compile: I had used (-1) which is seen as negative one. Bracketting the minus doesn't help as it prefixes it and makes 1 its first parameter. In short, what is the point free version of this? dec :: Num a => a -> a dec x = x - 1 I believe you want the conveniently-named subtract

What is the equivalent to (+1) for the subtraction, since (-1) is seen as a negative number? [duplicate]

十年热恋 提交于 2019-12-01 13:44:10
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Currying subtraction I started my first haskell project that is not from a tutorial, and of course I stumble on the simplest things. I have the following code: moveUp y = modifyMVar_ y $ return . (+1) moveDn y = modifyMVar_ y $ return . (-1) It took me some time to understand why my code wouldn't compile: I had used (-1) which is seen as negative one. Bracketting the minus doesn't help as it prefixes it and

Is eta reduction possible?

社会主义新天地 提交于 2019-12-01 05:22:52
Is it possible to apply eta reduction in below case? let normalise = filter (\x -> Data.Char.isLetter x || Data.Char.isSpace x ) I was expecting something like this to be possible: let normalise = filter (Data.Char.isLetter || Data.Char.isSpace) ...but it is not Your solution doesn't work, because (||) works on Bool values, and Data.Char.isLetter and Data.Char.isSpace are of type Char -> Bool . pl gives you: $ pl "f x = a x || b x" f = liftM2 (||) a b Explanation: liftM2 lifts (||) to the (->) r monad, so it's new type is (r -> Bool) -> (r -> Bool) -> (r -> Bool) . So in your case we'll get:

Point-free style and using $

試著忘記壹切 提交于 2019-12-01 03:36:00
How does one combine using $ and point-free style? A clear example is the following utility function: times :: Int -> [a] -> [a] times n xs = concat $ replicate n xs Just writing concat $ replicate produces an error, similarly you can't write concat . replicate either because concat expects a value and not a function. So how would you turn the above function into point-free style? You can use this combinator: (The colon hints that two arguments follow) (.:) :: (c -> d) -> (a -> b -> c) -> a -> b -> d (.:) = (.) . (.) It allows you to get rid of the n : time = concat .: replicate You can easily