pointfree

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

喜欢而已 提交于 2019-12-22 02:03:06
问题 According to pointfree : \x -> (x, x) is equivalent to: join (,) What is the derivation that shows this? 回答1: 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

Tacit function composition in Haskell

半城伤御伤魂 提交于 2019-12-22 02:00:59
问题 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

simple Haskell functions in point-free style

☆樱花仙子☆ 提交于 2019-12-20 19:04:27
问题 I am trying to understand how to convert functions to point-free notation in Haskell. I saw this example, but it is more complicated than what I am looking for. I feel like I understand the logic behind it, but when I am trying to execute some simple examples in code I am getting compile errors. I want to try and write this function in point-free style: f x = 5 + 8/x which I rearranged as f x = (+) 5 $ (/) 8 x So, I thought it might be something like this: f = (+) 5 $ (/) 8 but when I run

Performance Implications of Point-Free style

十年热恋 提交于 2019-12-20 17:34:26
问题 I’m taking my first baby-steps in learning functional programing using F# and I’ve just come across the Forward Pipe (|>) and Forward Composition (>>) operators. At first I thought they were just sugar rather than having an effect on the final running code (though I know piping helps with type inference). However I came across this SO article: What are advantages and disadvantages of “point free” style in functional programming? Which has two interesting and informative answers (that instead

Dot operator in haskell with multi-parameter functions

烂漫一生 提交于 2019-12-18 13:27:15
问题 I want to write a function point-free in haskell, to keep things simple lets say I want to make this function: maxmin :: Ord a => a -> a -> a -> a maxmin a b c = max a (min b c) I can improve this to maxmin a b = (max a) . (min b) but is there any way to get rid of a and b? 回答1: I wouldn't say this is simplier but here you go: maxmin :: Ord a => a -> a -> a -> a maxmin = (. min) . (.) . max (Generated with pl tool from lambdabot http://www.haskell.org/haskellwiki/Pointfree) lambdabot> pl

Style vs Performance Using Vectors

♀尐吖头ヾ 提交于 2019-12-18 04:35:25
问题 Here's the code: {-# LANGUAGE FlexibleContexts #-} import Data.Int import qualified Data.Vector.Unboxed as U import qualified Data.Vector.Generic as V {-# NOINLINE f #-} -- Note the 'NO' --f :: (Num r, V.Vector v r) => v r -> v r -> v r --f :: (V.Vector v Int64) => v Int64 -> v Int64 -> v Int64 --f :: (U.Unbox r, Num r) => U.Vector r -> U.Vector r -> U.Vector r f :: U.Vector Int64 -> U.Vector Int64 -> U.Vector Int64 f = V.zipWith (+) -- or U.zipWith, it doesn't make a difference main = do let

Filter subsets based on length?

安稳与你 提交于 2019-12-17 20:20:20
问题 Trying to extract the subsets with length k using filter. Not sure how to approach it? The list has 100 elements . subsets :: [a] -> [[a]] subsets [] = [[]] subsets (x:xs) = [zs | ys <- subsets xs, zs <- [ys, (x:ys)]] If i use filter this is what i thought it would be: filter (length(3)) subsets [1,2,3,4,5] But i'm probably wrong. If there is a different approach rather than filter? I'm new to haskell so not exactly sure. 回答1: When I get stuck with a little confusion in filtering, I go a

How to do pointfree style with long parameter list

烂漫一生 提交于 2019-12-13 13:02:55
问题 I've got a function that creates an Async workflow, and the function that takes 10 arguments in curry style. e.g. let createSequenceCore a b c d e f g h i j = async { ... } I want to create another function to start that workflow, so I've got let startSequenceCore a b c d e f g h i j = Async.StartImmediate (createSequenceCore a b c d e f g h i j) Is there any way I can get rid of those redundant parameters? I tried the << operator, but that only lets me remove one. let startSequenceCore a b c

Why does this point-free F# function behave differently from the non-point-free version?

扶醉桌前 提交于 2019-12-12 16:24:54
问题 Consider the following F#:- type TestClass() = let getValFromMap m k = Map.find k m let addToMap map k i = map |> Map.add k i let mutable someMap : Map<string,int> = Map.empty let getValFromMapPartial key = getValFromMap someMap key let getValFromMapPartialAndTacit = getValFromMap someMap member this.AddThenGet() = someMap <- addToMap someMap "A" 10 let value = getValFromMapPartial "A" printfn "Value from partial = %i" value // prints out let value = getValFromMapPartialAndTacit "A" // throws

Pondering name of pattern seen in Elm and if other similar cases

好久不见. 提交于 2019-12-12 11:20:23
问题 I'm currently a student of FP. As I look around different syntax offer by different functional languages, I've come across a pattern in Elm sample code. I am curious about it. Here is the sample code myList = [{foo = "bar1"},{foo = "bar2"}] foos = myList |> List.map .foo In the last line here, List.map is being passed .foo . I believe this style is called point-free but what about the specific pattern of passing in an attribute to the List.map function? Is this a more common thing? Is it