In Haskell, we would return such results as a tuple. The expression negate . Because of the Nothing case, the type of lookup is actually … Apply a function to all list elements.

Example: partition:: (a-> Bool)-> [a]-> ([a], [a]) How can I convert the result of partition such that it meets the needs of (++)? Tuples are handy when you want to return more than one value from a function. On the other hand, functions cannot emit values in a compatible way, and thus must return pairs instead. Note that this lookup function is not the one included in the Prelude; it is self-defined within the .hs file. f must take as its parameter a value that has the same type as g 's return value. Although it is a virtual concept, but in real-world programs, every function that we define in Haskell use higher-order mechanism to provide output. The maybe function takes a default value, a function, and a Maybe value. You can't convert a pair to something that fits a function that accepts two arguments in curried form. For example, Examples Expand. If you get a chance to look into the library function of Haskell, then you will find that most of the library functions have been written in higher order manner. The function takes the element and returns Nothing if it is done producing the list or returns Just (a,b), in which case, a is a prepended to the list and b is used as the next element in a recursive call. (Intuitively, we can think of types as sets of values.) Convert a list of foos into a list of bars.

If the Maybe value is Nothing, the function returns the default value.Otherwise, it applies the function to the value inside the Just and returns the result.. Basic usage: >>> maybe False odd (Just 3) True >>> maybe False odd Nothing False Read an integer from a string using readMaybe.

Apply a function to just some elements of a list. The unfoldr function is a `dual' to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. In many languages, returning two or more things at once often requires wrapping them up in a single-purpose data structure, maybe one that only gets used in that function. Answer. Assuming you only want to apply function f to elements for which function p returns true, you can do this: map (\ x-> if p x then f x else x) xs. Every value has an associated type. map my_function xs. (* 3) returns a function that takes a number, multiplies it by 3 and then negates it. So the resulting function takes a parameter of the same type that g takes and returns a value of the same type that f returns. Because Haskell is a purely functional language, all computations are done via the evaluation of expressions (syntactic terms) to yield values (abstract entities that we regard as answers). If the string is found, the value associated with it is returned, but if it is not found, Nothing is returned. I want to create Haskell function with different return value from its parameter, example: I want function isOdd 3 return value either True or False. 2 Values, Types, and Other Goodies.