Haskell equivalent of SML-like "return a function"

In summary, Haskell and SML are both functional programming languages, but Haskell has more advanced features and is both statically and strongly typed. "Returning a function" means that a function is being returned as the result of another function in both languages. To define a function that returns a function in Haskell, the "currying" technique is used. Some advantages of using Haskell over SML for functional programming include a more advanced type system and a larger community with more resources available. An example of a function in SML that returns a function is "add x = fn y => x + y" and its equivalent in Haskell is "add x = \y -> x + y".
  • #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?
 
Technology news on Phys.org
  • #2
Code:
foo :: a -> Int -> Int
foo x = \x -> x+1

This can be written more concisely as:

Code:
foo = const (+1)
 
  • #3
Hi,People! I need to define in haskell a function which ignore all doublings . For example if i type a string "abaacccdee" in the end i get the string "abacde".
 

What is the difference between Haskell and SML?

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.

What does it mean to "return a function" in SML and Haskell?

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.

How do you define a function in Haskell that is equivalent to an SML function that returns a 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.

What are some advantages of using Haskell over SML for functional programming?

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.

Can you give an example of a function in SML and its equivalent in Haskell that returns a function?

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

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
940
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
8
Views
792
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
3K
Back
Top