pointfree

How can I understand “(.) . (.)”?

余生长醉 提交于 2019-11-28 17:13:54
I believe I understand fmap . fmap for Functors, but on functions it's hurting my head for months now. I've seen that you can just apply the definition of (.) to (.) . (.) , but I've forgot how to do that. When I try it myself it always turns out wrong: (.) f g = \x -> f (g x) (.) (.) (.) = \x -> (.) ((.) x) \x f -> (.) ((.) x) f \x f y -> (((.)(f y)) x) \x f y g-> (((.)(f y) g) x) \x f y g-> ((f (g y)) x) \x f y g-> ((f (g y)) x):: t2 -> (t1 -> t2 -> t) -> t3 -> (t3 -> t1) -> t If "just applying the definition" is the only way of doing it, how did anybody come up with (.) . (.) ? There must

What are advantages and disadvantages of “point free” style in functional programming?

…衆ロ難τιáo~ 提交于 2019-11-28 15:52:11
I know that in some languages (Haskell?) the striving is to achieve point-free style, or to never explicitly refer to function arguments by name. This is a very difficult concept for me to master, but it might help me to understand what the advantages (or maybe even disadvantages) of that style are. Can anyone explain? I believe the purpose is to be succinct and to express pipelined computations as a composition of functions rather than thinking of threading arguments through. Simple example (in F#) - given: let sum = List.sum let sqr = List.map (fun x -> x * x) Used like: > sum [3;4;5] 12 >

Count frequency of each element in a list

我怕爱的太早我们不能终老 提交于 2019-11-28 10:14:24
I try to write a program which will count the frequency of each element in a list. In: "aabbcabb" Out: [("a",3),("b",4),("c",1)] You can view my code in the following link: http://codepad.org/nyIECIT2 In this code the output of unique function would be like this In: "aabbcabb" Out: "abc" Using the output of unique we wil count the frequency of the target list. You can see the code here also: frequencyOfElt xs=ans where ans=countElt(unique xs) xs unique []=[] unique xs=(head xs):(unique (filter((/=)(head xs))xs)) countElt ref target=ans' where ans'=zip ref lengths lengths=map length $ zipWith($

Variadic compose function?

纵饮孤独 提交于 2019-11-28 09:11:29
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, KindSignatures #-} class Comp a b c d | c -> d where comp :: (a -> b) -> c -> d instance Comp a b (a :: *) (b ::

Can this be expressed in point free style?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 04:55:27
问题 Given the following expression to sum an IEnumerable of numbers: let sum l = l |> Seq.reduce(+) //version a is it possible to eliminate the argument--like so? let sum = Seq.reduce(+) //version b I get an error from the F# compiler (FS0030) and I seem to recall having seen something about an "eta conversion" being involved but unfortunately my knowledge of lambda calc is too limited to follow how eta conversion is involved. Can the argument be eliminated as in version b? Would someone please

Writing in pointfree style f x = g x x

妖精的绣舞 提交于 2019-11-28 00:43:49
I am learning Haskell. I'm sorry for asking a very basic question but I cant seem to find the answer. I have a function f defined by : f x = g x x where g is an already defined function of 2 arguments. How do I write this pointfree style? Edit : without using a lambda expression. Thanks ehird f can be written with Control.Monad.join : f = join g join on the function monad is one of the primitives used when constructing point-free expressions , as it cannot be defined in a point-free style itself (its SKI calculus equivalent, SII — ap id id in Haskell — doesn't type) . This is known as "W"

How can I understand “(.) . (.)”?

谁说我不能喝 提交于 2019-11-27 20:05:56
问题 I believe I understand fmap . fmap for Functors, but on functions it's hurting my head for months now. I've seen that you can just apply the definition of (.) to (.) . (.) , but I've forgot how to do that. When I try it myself it always turns out wrong: (.) f g = \x -> f (g x) (.) (.) (.) = \x -> (.) ((.) x) \x f -> (.) ((.) x) f \x f y -> (((.)(f y)) x) \x f y g-> (((.)(f y) g) x) \x f y g-> ((f (g y)) x) \x f y g-> ((f (g y)) x):: t2 -> (t1 -> t2 -> t) -> t3 -> (t3 -> t1) -> t If "just

Point-free in Haskell

梦想的初衷 提交于 2019-11-27 13:02:46
问题 I have this code that I want to make point-free; (\k t -> chr $ a + flip mod 26 (ord k + ord t -2*a)) How do I do that? Also are there some general rules for point free style other than "think about this amd come up with something"? 回答1: To turn a function func x y z = (some expression in x, y and z) into point-free form, I generally try to follow what is done to the last parameter z and write the function as func x y z = (some function pipeline built using x and y) z Then I can cancel out

Anyone ever flip (<$>)

五迷三道 提交于 2019-11-27 11:44:16
问题 I found defining the following (%) = flip fmap I can write code like this: readFile "/etc/passwd" % lines % filter (not . null) To me it makes more sense than the alternative: filter (not . null) <$> lines <$> readFile "/etc/passwd" Obviously, it's just a matter of order. Does anyone else do this? Is there a valid reason not to write code like this? 回答1: Your operator (%) is exactly the operator (<&>) from the lens package. It can be imported with: import Control.Lens.Operators ((<&>)) 回答2:

How do I re-write a Haskell function of two argument to point-free style

ぐ巨炮叔叔 提交于 2019-11-27 11:06:34
问题 I have the following function in Haskell agreeLen :: (Eq a) => [a] -> [a] -> Int agreeLen x y = length $ takeWhile (\(a,b) -> a == b) (zip x y) I'm trying to learn how to write 'idiomatic' Haskell, which seem to prefer using . and $ instead of parenthesis, and also to prefer pointfree code where possible. I just can't seem to get rid of mentioning x and y explicitly. Any ideas? I think I'd have the same issue with pointfreeing any function of two arguments. BTW, this is just in pursuit of