- #1
Dragonfall
- 1,030
- 4
I want do the Haskell equivalent of SML-like "return a function"
fun foo x = fn x=>x+1
How do I do this in Haskell?
fun foo x = fn x=>x+1
How do I do this in Haskell?
foo :: a -> Int -> Int
foo x = \x -> x+1
foo = const (+1)
Haskell and SML are both functional programming languages, but they have some key differences. SML is statically typed, while Haskell is both statically and strongly typed. Haskell also has more advanced features, such as type inference and lazy evaluation, which makes it more expressive and concise compared to SML.
In both SML and Haskell, functions are first-class citizens, which means they can be treated as values and passed around like any other data type. "Returning a function" simply means that a function is being returned as the result of another function.
In Haskell, functions are defined using the "fun x =>" syntax, whereas in SML, the "fun x =>" syntax is used to define anonymous functions. To define a function that returns a function in Haskell, you would use the "currying" technique, where a function with multiple arguments is defined as a series of functions, each taking one argument and returning a function that takes the next argument.
Haskell has a more advanced type system and supports features like type classes and higher-order functions, making it more flexible and powerful for functional programming. It also has a large and active community, with a vast collection of libraries and tools available for use.
Here is an example of a function in SML that returns a function:
fun add x = fn y => x + y;
And here is its equivalent in Haskell:
add x = \y -> x + y