Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does backslash space mean in Haskell

Tags:

lambda

haskell

I know that something like \x->x+1 is an anonymous function definition that gets an x and adds one to it. But I saw the expression return x = (\ r -> x) when I was reading an article here.

What does (\ r -> x) mean? Why after backslash is empty?

like image 899
qartal Avatar asked Nov 30 '25 03:11

qartal


1 Answers

There is no difference: \ r -> x or \r -> x have the same meaning, much as 1+1 and 1 + 1 have the same meaning. Whitespace after \ is irrelevant.

So, return x is just \r -> x, which is a function which takes a parameter r, ignores it, and yields x.

Moreover, since r gets ignored, we tend to write \ _ -> x (or const x - which is defined as const x _ = x) instead.

like image 101
chi Avatar answered Dec 02 '25 16:12

chi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!