function-composition

Dot Operator in Haskell: need more explanation

混江龙づ霸主 提交于 2019-11-27 06:35:19
I'm trying to understand what the dot operator is doing in this Haskell code: sumEuler = sum . (map euler) . mkList The entire source code is below. My understanding The dot operator is taking the two functions sum and the result of map euler and the result of mkList as the input. But, sum isn't a function it is the argument of the function, right? So what is going on here? Also, what is (map euler) doing? Code mkList :: Int -> [Int] mkList n = [1..n-1] euler :: Int -> Int euler n = length (filter (relprime n) (mkList n)) sumEuler :: Int -> Int sumEuler = sum . (map euler) . mkList Put simply,

Variadic compose function?

两盒软妹~` 提交于 2019-11-27 02:41:48
问题 I'm trying to write a variadic function composition function. Which is basically the (.) except that the second argument function is variadic. This should allow expressions like: map even . zipWith (+) or just map even . zipWith Currently what I've reached works if I add IncoherentInstances and requires a non-polymorphic instance for the first argument function. {-# LANGUAGE FlexibleInstances, OverlappingInstances, MultiParamTypeClasses, FunctionalDependencies, UndecidableInstances,

Functional composition with multi-valued functions in haskell?

谁说我不能喝 提交于 2019-11-26 21:47:20
问题 I was wondering if it was possible to do functional composition with functions that take more than one argument. I want to be able to do something like this x = (+3).(*) setting x equal to a function that adds three to the product of two numbers. 回答1: There are multiple ways to do it, but they're all somewhat awkward. ((+3).) . (*) ≡ fmap (+3) . (*) ≡ curry $ (+3) . uncurry (*) ≡ \l r -> l*r + 3 Oh, wait, this was the signature where there's also a compact definition, guess what it's called..

Haskell function composition operator of type (c→d) → (a→b→c) → (a→b→d)

随声附和 提交于 2019-11-26 21:42:23
Ordinary function composition is of the type (.) :: (b -> c) -> (a -> b) -> a -> c I figure this should generalize to types like: (.) :: (c -> d) -> (a -> b -> c) -> a -> b -> d A concrete example: calculating difference-squared. We could write diffsq a b = (a - b) ^ 2 , but it feels like I should be able to compose the (-) and (^2) to write something like diffsq = (^2) . (-) . I can't, of course. One thing I can do is use a tuple instead of two arguments to (-) , by transforming it with uncurry , but this isn't the same. Is it possible to do what I want? If not, what am I misunderstanding

How to multiply functions in python?

我们两清 提交于 2019-11-26 20:20:32
问题 def sub3(n): return n - 3 def square(n): return n * n It's dead easy to compose functions in python: >>> my_list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> [square(sub3(n)) for n in my_list] [9, 4, 1, 0, 1, 4, 9, 16, 25, 36] Unfortunately, when wanting to use the composition as a key , it's kind of lame: >>> sorted(my_list, key=lambda n: square(sub3(n))) [3, 2, 4, 1, 5, 0, 6, 7, 8, 9] This should really just be sorted(my_list, key=square*sub3) , because heck, function __mul__ isn't used for anything

Haskell function composition (.) and function application ($) idioms: correct use

一曲冷凌霜 提交于 2019-11-26 18:21:54
I have been reading Real World Haskell , and I am nearing the end, but a matter of style has been niggling at me to do with the (.) and ($) operators. When you write a function that is a composition of other functions you write it like: f = g . h But when you apply something to the end of those functions I write it like this: k = a $ b $ c $ value But the book would write it like this: k = a . b . c $ value Now, to me they look functionally equivalent, they do the exact same thing in my eyes. However, the more I look, the more I see people writing their functions in the manner that the book

What does (f .) . g mean in Haskell?

泪湿孤枕 提交于 2019-11-26 14:23:58
I have seen a lot of functions being defined according to the pattern (f .) . g . For example: countWhere = (length .) . filter duplicate = (concat .) . replicate concatMap = (concat .) . map What does this mean? The dot operator (i.e. (.) ) is the function composition operator. It is defined as follows: infixr 9 . (.) :: (b -> c) -> (a -> b) -> a -> c f . g = \x -> f (g x) As you can see it takes a function of type b -> c and another function of type a -> b and returns a function of type a -> c (i.e. which applies the first function to the result of the second function). The function

function composition in C++ / C++11

旧巷老猫 提交于 2019-11-26 13:58:32
问题 I am currently coding some cryptographic algorithms in C++11 that require a lot of function compositions. There are 2 types of composition I have to deal with : Compose a function on itself a variable number of times. Mathematically, for a certain function F, F^n(x) = (F^{n-1} o F)(x) = F^{n-1}(F(x)). Compose different functions together. For example, for some functions f,g,h,i,j and k of the same type, I'll have f(g(h(i(j(k(x)))))). In my case, I'm using the following definition of F : const

Dot Operator in Haskell: need more explanation

微笑、不失礼 提交于 2019-11-26 12:05:41
问题 I\'m trying to understand what the dot operator is doing in this Haskell code: sumEuler = sum . (map euler) . mkList The entire source code is below. My understanding The dot operator is taking the two functions sum and the result of map euler and the result of mkList as the input. But, sum isn\'t a function it is the argument of the function, right? So what is going on here? Also, what is (map euler) doing? Code mkList :: Int -> [Int] mkList n = [1..n-1] euler :: Int -> Int euler n = length

runST and function composition

懵懂的女人 提交于 2019-11-26 11:47:14
Why does this typecheck: runST $ return $ True While the following does not: runST . return $ True GHCI complains: Couldn't match expected type `forall s. ST s c0' with actual type `m0 a0' Expected type: a0 -> forall s. ST s c0 Actual type: a0 -> m0 a0 In the second argument of `(.)', namely `return' In the expression: runST . return The short answer is that type inference doesn't always work with higher-rank types. In this case, it is unable to infer the type of (.) , but it type checks if we add an explicit type annotation: > :m + Control.Monad.ST > :set -XRankNTypes > :t (((.) :: ((forall