pointfree

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

我的未来我决定 提交于 2019-11-27 09:28:40
问题 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? 回答1: 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

Partial function application for a non-symmetric operator using point-free style in F#?

流过昼夜 提交于 2019-11-27 07:48:13
问题 How can I create a partial function application for a non-symmetric operator such as the modulus operator with regards to the first argument without any argument names in F#? My first attempt was let mod10 = (%) 10 which of course translates to mod10(x) = 10 mod x instead of the desired mod10(x) = x mod 10 . Certainly I could write let mod10 x = (%)x 10 but I'd like to not have to name the argument so is there some placeholder that can be used, something like let mod10 = (%)_ 10 ? 回答1: You

Writing in pointfree style f x = g x x

孤者浪人 提交于 2019-11-27 04:44:48
问题 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 回答1: 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

Making numeric functions an instance of Num?

橙三吉。 提交于 2019-11-27 03:24:00
问题 I want to be able to compose numeric functions in haskell using binary operators. So, for example, with unary numeric functions: f*g should translate to: \x -> (f x)*(g x) and similarly for addition. Making your own operator to do this is pretty straightforward, but I'd really like to just make Num a => a -> a functions an instance of Num, but I'm not sure how to do so. I'd also like to make this arity generic, but that might be too much trouble for how difficult it is to do arity generic

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,

What is “point free” style (in Functional Programming)?

狂风中的少年 提交于 2019-11-26 21:49:22
A phrase that I've noticed recently is the concept of "point free" style... First, there was this question, and also this one . Then, I discovered here they mention "Another topic that may be worth discussing is the authors' dislike of point free style." What is "point free" style? Can someone give a concise explanation? Does it have something to do with "automatic" currying? To get an idea of my level - I've been teaching myself Scheme, and have written a simple Scheme interpreter... I understand what "implicit" currying is, but I don't know any Haskell or ML. Dario Just look at the Wikipedia

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

Currying 3 Arguments in Haskell

余生颓废 提交于 2019-11-26 20:50:38
I'm having trouble with currying a function to remove three arguments in Haskell. Disclaimer: Not Coursework, I was asked this question by someone struggling with this today and it's been bugging me. The custom types/functions we were given were (can only remember types) type MyThing = (Char, String) type MyThings = [MyThing] funcA :: MyThings -> String -> String funcB :: MyThings -> String -> Int -> String We started with: funcB as str n = iterate (funcA as) str !! n And reduced it down as follows: funcB as str n = iterate (funcA as) str !! n funcB as str = (!!) . (iterate (funcA as)) str

How to use (->) instances of Monad and confusion about (->)

人走茶凉 提交于 2019-11-26 16:13:51
At different questions I've found hints in comments concerning using the (->) instance of Monads e.g. for realizing point-free style. As for me, this is a little too abstract. Ok, I've seen Arrow instances on (->) and it seems to me, that (->) can be used in instance notations but not in type declarations (that would alone be stuff for another question). Has anyone examples using (->) as instance of Monad? Or a good link? Sorry if this question may already have been discussed here, but searching for " (->) Monad instance" gives you many many hits as you can imagine ... since nearly every