pointfree

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

泪湿孤枕 提交于 2019-11-26 14:23:58
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? 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 type a -> c (i.e. which applies the first function to the result of the second function). The function

How is this fibonacci-function memoized?

ε祈祈猫儿з 提交于 2019-11-26 10:08:50
By what mechanism is this fibonacci-function memoized? fib = (map fib' [0..] !!) where fib' 1 = 1 fib' 2 = 1 fib' n = fib (n-2) + fib (n-1) And on a related note, why is this version not? fib n = (map fib' [0..] !! n) where fib' 1 = 1 fib' 2 = 1 fib' n = fib (n-2) + fib (n-1) Will Ness The evaluation mechanism in Haskell is by-need : when a value is needed, it is calculated, and kept ready in case it is asked for again. If we define some list, xs=[0..] and later ask for its 100th element, xs!!99 , the 100th slot in the list gets "fleshed out", holding the number 99 now, ready for next access.

Composing function composition: How does (.).(.) work?

ε祈祈猫儿з 提交于 2019-11-26 09:46:15
问题 (.) takes two functions that take one value and return a value: (.) :: (b -> c) -> (a -> b) -> a -> c Since (.) takes two arguments, I feel like (.).(.) should be invalid, but it\'s perfectly fine: (.).(.) :: (b -> c) -> (a -> a1 -> b) -> a -> a1 -> c What is going on here? I realize this question is badly worded...all functions really just take one argument thanks to currying. Maybe a better way to say it is that the types don\'t match up. 回答1: Let's first play typechecker for the mechanical

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

冷暖自知 提交于 2019-11-26 08:03:53
问题 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.

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

Currying 3 Arguments in Haskell

主宰稳场 提交于 2019-11-26 07:42:49
问题 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

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

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

别说谁变了你拦得住时间么 提交于 2019-11-26 04:45:49
问题 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

How is this fibonacci-function memoized?

寵の児 提交于 2019-11-26 01:56:49
问题 By what mechanism is this fibonacci-function memoized? fib = (map fib\' [0..] !!) where fib\' 1 = 1 fib\' 2 = 1 fib\' n = fib (n-2) + fib (n-1) And on a related note, why is this version not? fib n = (map fib\' [0..] !! n) where fib\' 1 = 1 fib\' 2 = 1 fib\' n = fib (n-2) + fib (n-1) 回答1: The evaluation mechanism in Haskell is by-need : when a value is needed, it is calculated, and kept ready in case it is asked for again. If we define some list, xs=[0..] and later ask for its 100th element,