Should I handle unexpected arguments of a function?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.