function-composition

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

自古美人都是妖i 提交于 2019-11-26 08:01:36
问题 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

Composing functions in python

天大地大妈咪最大 提交于 2019-11-26 06:37:14
问题 I have an array of functions and I\'m trying to produce one function which consists of the composition of the elements in my array. My approach is: def compose(list): if len(list) == 1: return lambda x:list[0](x) list.reverse() final=lambda x:x for f in list: final=lambda x:f(final(x)) return final This method doesn\'t seems to be working, help will be appreciated. (I\'m reversing the list because this is the order of composition I want the functions to be) 回答1: It doesn't work because all

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

非 Y 不嫁゛ 提交于 2019-11-26 05:53:46
问题 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? 回答1: 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

runST and function composition

折月煮酒 提交于 2019-11-26 03:34:47
问题 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 回答1: 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

What is the difference between . (dot) and $ (dollar sign)?

我与影子孤独终老i 提交于 2019-11-25 22:38:32
问题 What is the difference between the dot (.) and the dollar sign ($) ? As I understand it, they are both syntactic sugar for not needing to use parentheses. 回答1: The $ operator is for avoiding parentheses. Anything appearing after it will take precedence over anything that comes before. For example, let's say you've got a line that reads: putStrLn (show (1 + 1)) If you want to get rid of those parentheses, any of the following lines would also do the same thing: putStrLn (show $ 1 + 1) putStrLn