pointfree

What are the prerequisites for a point-free function in Haskell

跟風遠走 提交于 2021-02-07 19:20:56
问题 I always thought that the prerequisites for a pointfree function were to get the function arguments to the end of the definition. E.g. -- This can be made pointfree quite easily: let lengths x = map length x let lengths' = map length -- However this cannot: let lengthz x = length `map` x -- let lengthz' = length `map` (parse error) I originally came across this reading this question. There we have this example: agreeLen :: (Eq a) => [a] -> [a] -> Int agreeLen x y = length $ takeWhile id $

Can this function be written in point-free style? If not, why?

Deadly 提交于 2020-08-03 15:20:33
问题 One related question is this, but some of the answer say that almost anything can be made point free, so what is wrong with this function? \[x] -> x http://pointfree.io/ doesn't seem to be able to write it in point-free style. Does this mean that it cannot be written that way? If so, what is the theoretical reason for it? I can only observe that the function above is a "crippled" version of head (or last , fwiw) which can only operate on singleton lists. Indeed, applied on non singleton lists

Is there a pretty/glib way to restrict a curry-ed function to the graph of another function in Haskell?

眉间皱痕 提交于 2020-05-31 04:55:34
问题 I finally got bored enough in quarantine to start learning Haskell today and I'm really enjoying myself, I really love the aesthetics of the language. Hopefully this question doesn't inspire any hate if it has already been posted here before, etc. It's very simple, but I'm an absolute beginner. I've been trying to understand how to do some simple things elegantly (or at least in a so-called "point free" way) with the language and came upon the problem of how to describe in a clean way the

When can eta reduction change a function's type?

喜欢而已 提交于 2020-01-21 07:03:51
问题 What exactly is going on with the following? > let test = map show > :t test test :: [()] -> [String] > :t (map show) (map show) :: Show a => [a] -> [String] I am wondering how I failed to notice this before? I actually encountered the problem with "map fromIntegral" rather than show - my code doesn't compile with the pointfree form, but works fine without eta reduction. Is there a simple explanation of when eta reduction can change the meaning of Haskell code? 回答1: This is the monomorphism

Haskell - apply tuple of functions to tuple of values?

房东的猫 提交于 2020-01-02 05:19:08
问题 I have a tuple of values representing some state, and want to translate it by an addition (shift). My values are a longer version of ( Int, [Int], Int), and I want something conceptually (but not literally) like this: shift n = ??? (+n) (id, map, id) -- simple(?) which would be equivalent to: shift n (a, b, c) = (a+n, map (+n) b, c+n) I am happy to just go with this explicit function usage, but wondered it there was a more idiomatic point-free version using Applicative or Arrows or ..., or if

Point-free style and using $

早过忘川 提交于 2019-12-30 08:18:16
问题 How does one combine using $ and point-free style? A clear example is the following utility function: times :: Int -> [a] -> [a] times n xs = concat $ replicate n xs Just writing concat $ replicate produces an error, similarly you can't write concat . replicate either because concat expects a value and not a function. So how would you turn the above function into point-free style? 回答1: You can use this combinator: (The colon hints that two arguments follow) (.:) :: (c -> d) -> (a -> b -> c) -

How are point-free functions actually “functions”?

有些话、适合烂在心里 提交于 2019-12-22 05:39:07
问题 Conal here argues that nullary-constructed types are not functions. However, point-free functions are described as such for example on Wikipedia, when they take no explicit arguments in their definitions, and it seemingly is rather a property of currying. How exactly are they functions? Specifically: how are f = map and f = id . map different in this context? As in, f = map is simply just a binding to a value that happens to be a function where f simply "returns" map (similar to how f = 2

Is there a point-free way to convert a conditional check into a Maybe type of the input?

匆匆过客 提交于 2019-12-22 05:18:05
问题 I am just working through some simple exercises in haskell and was wondering if there was a point-free way of converting an if-then-else statement into a Maybe type: Nothing being returned if the condition is false, and Just the input if the condition is true. In short, given some: maybeIf :: (a -> Bool) -> a -> Maybe a maybeIf cond a = if cond a then Just a else Nothing Is there an implementation that is point-free with respect to a ? I've also been looking at a more concrete version, a ->

Confusion about currying and point free style in Haskell

时光怂恿深爱的人放手 提交于 2019-12-22 04:07:38
问题 I was trying to implement the function every :: (a -> IO Bool) -> [a] -> IO Bool which was the topic for this question. I tried to do this without explicit recursion . I came up with the following code every f xs = liftM (all id) $ sequence $ map f xs My function didn't work since it wasn't lazy (which was required in the question), so no upvotes there :-). However, I did not stop there. I tried to make the function point-free so that it would be shorter (and perhaps even cooler). Since the