How do I create a function that returns a function that takes two arguments, a function and an input variable x?
-
Given the following function definitions which represent the first four positive integers as the function names one, two, three, and four, and assuming similar definitions for every positive integer, what is the type definition and Haskellcode for a function called plus that will take as arguments two such functions representing integers and return as a result a function that represents the sum of the two input integers. For example, the application (plus one two) should evaluate to a function that takes two arguments (f and x) and returns the value (f (f (f x))). one f x = f x two f x = f (f x) three f x = f (f (f x))
-
Answer:
I'll replace your functions one, two, three with f defined as: f :: (Eq a, Num a) => a -> (b -> b) -> b -> b f 1 g = g f n g = (f (n-1) g) . g so: one = f 1 two = f 2 three = f 3 ... your add is just function composition: add f g = f . g
Gregory Popovitch at Quora Visit the source
Other answers
This is the "function power" function: [math]f^n(x) = f (f (\dots f(x))) = (f \cdot f \cdot \dots \cdot f)(x) [/math] [math]f^0(x) = id[/math] fpower :: Int -> (a -> a) -> (a -> a)fpower n f = foldl (.) id fs where fs = replicate n fone = fpower 1two = fpower 2three = fpower 3...mul_1024 = fpower 10 (*2)mul_1 = fpower 0 (*2)
Maxim Borisyak
Related Q & A:
- How do I create a Cocoa Touch Framework?Best solution by stackoverflow.com
- How can i create a mobile application server?Best solution by Stack Overflow
- How do I create a digital signature and how do I use it?Best solution by support.office.com
- How do I create a new Yahoo screen name if I already have one?Best solution by Yahoo! Answers
- How Can I Create an XML to Create a Menu?Best solution by Drupal Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.