pointfree

Is eta reduction possible?

人盡茶涼 提交于 2019-12-01 03:16:58
问题 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 回答1: 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

What is a general scheme for writing a function in pointfree style?

不打扰是莪最后的温柔 提交于 2019-12-01 00:30:27
问题 I am working through the 20 Intermediate Haskell Exercises at the moment, which is quite a fun exercise. It involves implementing various instances of the typeclasses Functor and Monad (and functions that takes Functor s and Monad s as arguments) but with cute names like Furry and Misty to disguise what we're doing (makes for some interesting code). I've been trying to do some of this in a point-free style, and I wondered if there's a general scheme for turning a point-ful (?) definition into

Is there a better way to express the absolute error function in point-free notation?

为君一笑 提交于 2019-11-30 07:09:30
问题 In pointful notation: absoluteError x y = abs (x-y) An unclear example in pointfree notation: absoluteError' = curry (abs . uncurry (-)) 回答1: Here's how you could derive it yourself, in small steps: absoluteError x y = abs (x-y) = abs ((-) x y) = abs ( ((-) x) y) = (abs . (-) x) y = ( (abs .) ((-) x) ) y = = ( (abs .) . (-) ) x y so, by eta-reduction, if f x y = g x y we conclude f = g . Further, using _B = (.) for a moment, (abs .) . (-) = _B (abs .) (-) = _B (_B abs) (-) = (_B . _B) abs (-)

How to turn a Ruby method into a block?

会有一股神秘感。 提交于 2019-11-30 06:38:19
Is there a way to simplify the following code? filenames is a list of filenames (strings), e.g. ["foo.txt", "bar.c", "baz.yaml"] filenames.map { |f| File.size(f) } Is there any way to turn "File.size" into a proc or block? For methods on existing objects, I can do &:method . Is there something analogous for module level methods? You can use Object#method(method_name) : filenames.map(&File.method(:size)) filesize = proc { |f| File.size(f) } filenames.map(&filesize) Stdlib's Pathname provides a more object oriented approach to files and directories. Maybe there's a way to refactor your filelist

Can this be expressed in point free style?

天大地大妈咪最大 提交于 2019-11-29 11:18:52
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 point me to literature that would explain an eta conversion and how it would come into play in this

How to turn a Ruby method into a block?

≡放荡痞女 提交于 2019-11-29 06:20:37
问题 Is there a way to simplify the following code? filenames is a list of filenames (strings), e.g. ["foo.txt", "bar.c", "baz.yaml"] filenames.map { |f| File.size(f) } Is there any way to turn "File.size" into a proc or block? For methods on existing objects, I can do &:method . Is there something analogous for module level methods? 回答1: You can use Object#method(method_name): filenames.map(&File.method(:size)) 回答2: filesize = proc { |f| File.size(f) } filenames.map(&filesize) 回答3: Stdlib's

Style vs Performance Using Vectors

风流意气都作罢 提交于 2019-11-29 05:55:49
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 iters = 100 dim = 221184 y = U.replicate dim 0 :: U.Vector Int64 let ans = iterate ((f y)) y !! iters

Is there a better way to express the absolute error function in point-free notation?

谁说我不能喝 提交于 2019-11-29 01:25:29
In pointful notation: absoluteError x y = abs (x-y) An unclear example in pointfree notation: absoluteError' = curry (abs . uncurry (-)) Here's how you could derive it yourself, in small steps: absoluteError x y = abs (x-y) = abs ((-) x y) = abs ( ((-) x) y) = (abs . (-) x) y = ( (abs .) ((-) x) ) y = = ( (abs .) . (-) ) x y so, by eta-reduction , if f x y = g x y we conclude f = g . Further, using _B = (.) for a moment, (abs .) . (-) = _B (abs .) (-) = _B (_B abs) (-) = (_B . _B) abs (-) = ((.) . (.)) abs (-) Daniel Wagner Here's a handful of ways. the old-fashioned: absoluteError = (abs .) .

Point-free in Haskell

让人想犯罪 __ 提交于 2019-11-28 19:33:12
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"? 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 the z s to get func x y = (some function pipeline built using x and y) Then repeating the process for y and x

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

£可爱£侵袭症+ 提交于 2019-11-28 17:53:55
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 writing good code; not some "use whatever it takes to make it pointfree" homework exercise. Thanks. (Added