Scott Wlaschin ThinkOfNumber example
up vote
0
down vote
favorite
I'm trying to find my way in functional programming with haskell.
I saw this nice video https://www.youtube.com/watch?v=vDe-4o8Uwl8 by Scott Wlaschin.
I'm tryng to implement his ThinkOfNumber example using haskell.

The only solution I found is this one:
thinkOfANumber numberYouThoughtOf = (x -> x - numberYouThoughtOf)
. (x -> x / numberYouThoughtOf)
. (x -> x - 1)
. (x -> x * x)
. (x -> x + 1)
So I have to invoke the function in this way, using 2 parameters
thinkOfANumber 13 13
in order to memorize the numberYouThoughtOf in the call chain.
I there a way to simplify the code, using a pipe like operator such as the "|>" operator in F# ?
Maybe I'm surely missing something :)
Regards.
Ivan
haskell
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
up vote
0
down vote
favorite
I'm trying to find my way in functional programming with haskell.
I saw this nice video https://www.youtube.com/watch?v=vDe-4o8Uwl8 by Scott Wlaschin.
I'm tryng to implement his ThinkOfNumber example using haskell.

The only solution I found is this one:
thinkOfANumber numberYouThoughtOf = (x -> x - numberYouThoughtOf)
. (x -> x / numberYouThoughtOf)
. (x -> x - 1)
. (x -> x * x)
. (x -> x + 1)
So I have to invoke the function in this way, using 2 parameters
thinkOfANumber 13 13
in order to memorize the numberYouThoughtOf in the call chain.
I there a way to simplify the code, using a pipe like operator such as the "|>" operator in F# ?
Maybe I'm surely missing something :)
Regards.
Ivan
haskell
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I mean, you could just write the whole thing in one linethink n x = (a * a - 1) / n - n where a = x + 1
– Mateen Ulhaq
yesterday
1
I think this is more appropriate for codereview.stackexchange.com
– hellow
yesterday
2
The F# pipe operator is available as(>>>)in theControl.Arrowmodule. You could also try using operator sections.
– bradrn
yesterday
@bradrn the>>>operator fromControl.Arrowis actually composition, whereas F# pipe is reverse application.
– Fyodor Soikin
yesterday
Thank you for clarifying @Fyodor Soikin - I'm not too familiar with F#. In that case(&)would indeed be the correct choice as you said below.
– bradrn
yesterday
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to find my way in functional programming with haskell.
I saw this nice video https://www.youtube.com/watch?v=vDe-4o8Uwl8 by Scott Wlaschin.
I'm tryng to implement his ThinkOfNumber example using haskell.

The only solution I found is this one:
thinkOfANumber numberYouThoughtOf = (x -> x - numberYouThoughtOf)
. (x -> x / numberYouThoughtOf)
. (x -> x - 1)
. (x -> x * x)
. (x -> x + 1)
So I have to invoke the function in this way, using 2 parameters
thinkOfANumber 13 13
in order to memorize the numberYouThoughtOf in the call chain.
I there a way to simplify the code, using a pipe like operator such as the "|>" operator in F# ?
Maybe I'm surely missing something :)
Regards.
Ivan
haskell
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm trying to find my way in functional programming with haskell.
I saw this nice video https://www.youtube.com/watch?v=vDe-4o8Uwl8 by Scott Wlaschin.
I'm tryng to implement his ThinkOfNumber example using haskell.

The only solution I found is this one:
thinkOfANumber numberYouThoughtOf = (x -> x - numberYouThoughtOf)
. (x -> x / numberYouThoughtOf)
. (x -> x - 1)
. (x -> x * x)
. (x -> x + 1)
So I have to invoke the function in this way, using 2 parameters
thinkOfANumber 13 13
in order to memorize the numberYouThoughtOf in the call chain.
I there a way to simplify the code, using a pipe like operator such as the "|>" operator in F# ?
Maybe I'm surely missing something :)
Regards.
Ivan
haskell
haskell
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited yesterday
Mark Seemann
181k33322554
181k33322554
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked yesterday
Ivan Saracino
62
62
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Ivan Saracino is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I mean, you could just write the whole thing in one linethink n x = (a * a - 1) / n - n where a = x + 1
– Mateen Ulhaq
yesterday
1
I think this is more appropriate for codereview.stackexchange.com
– hellow
yesterday
2
The F# pipe operator is available as(>>>)in theControl.Arrowmodule. You could also try using operator sections.
– bradrn
yesterday
@bradrn the>>>operator fromControl.Arrowis actually composition, whereas F# pipe is reverse application.
– Fyodor Soikin
yesterday
Thank you for clarifying @Fyodor Soikin - I'm not too familiar with F#. In that case(&)would indeed be the correct choice as you said below.
– bradrn
yesterday
add a comment |
I mean, you could just write the whole thing in one linethink n x = (a * a - 1) / n - n where a = x + 1
– Mateen Ulhaq
yesterday
1
I think this is more appropriate for codereview.stackexchange.com
– hellow
yesterday
2
The F# pipe operator is available as(>>>)in theControl.Arrowmodule. You could also try using operator sections.
– bradrn
yesterday
@bradrn the>>>operator fromControl.Arrowis actually composition, whereas F# pipe is reverse application.
– Fyodor Soikin
yesterday
Thank you for clarifying @Fyodor Soikin - I'm not too familiar with F#. In that case(&)would indeed be the correct choice as you said below.
– bradrn
yesterday
I mean, you could just write the whole thing in one line
think n x = (a * a - 1) / n - n where a = x + 1– Mateen Ulhaq
yesterday
I mean, you could just write the whole thing in one line
think n x = (a * a - 1) / n - n where a = x + 1– Mateen Ulhaq
yesterday
1
1
I think this is more appropriate for codereview.stackexchange.com
– hellow
yesterday
I think this is more appropriate for codereview.stackexchange.com
– hellow
yesterday
2
2
The F# pipe operator is available as
(>>>) in the Control.Arrow module. You could also try using operator sections.– bradrn
yesterday
The F# pipe operator is available as
(>>>) in the Control.Arrow module. You could also try using operator sections.– bradrn
yesterday
@bradrn the
>>> operator from Control.Arrow is actually composition, whereas F# pipe is reverse application.– Fyodor Soikin
yesterday
@bradrn the
>>> operator from Control.Arrow is actually composition, whereas F# pipe is reverse application.– Fyodor Soikin
yesterday
Thank you for clarifying @Fyodor Soikin - I'm not too familiar with F#. In that case
(&) would indeed be the correct choice as you said below.– bradrn
yesterday
Thank you for clarifying @Fyodor Soikin - I'm not too familiar with F#. In that case
(&) would indeed be the correct choice as you said below.– bradrn
yesterday
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
Operator & from Data.Function is the exact analog of the F# pipe:
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
& addOne
& squareIt
& subtractOne
& divideByTheNumberYouThoughtOf
Of course, if you like the pipe (I personally do), you can always define it in Haskell:
infixl 5 |>
x |> f = f x
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
|> addOne
|> squareIt
|> subtractOne
|> divideByTheNumberYouThoughtOf
Thank you. Really nice.
– Ivan Saracino
yesterday
add a comment |
up vote
0
down vote
Operator (>>=) from Control.Monad package may be able to imitate |> operator in F# with using Identity from Control.Monad.Identity like:
import Control.Monad.Identity;
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in runIdentity
$ pure numberYonThoughOf
>>= pure . addOne
>>= pure . squareIt
>>= pure . subtractOne
>>= pure . divideByTheNumberYouThoughtOf
Thank you. This is the kind of answer I was looking for. Regards. Ivan
– Ivan Saracino
yesterday
add a comment |
up vote
0
down vote
Fyodor Soikin is correct that & corresponds to F#'s |>, but my impression from reading Haskell code is that it's considered more idiomatic to use the $ operator and compose from right to left, like in mathematics:
thinkOfANumber numberYouThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYouThoughOf
in divideByTheNumberYouThoughtOf $ subtractOne $ squareIt $ addOne numberYouThoughOf
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Operator & from Data.Function is the exact analog of the F# pipe:
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
& addOne
& squareIt
& subtractOne
& divideByTheNumberYouThoughtOf
Of course, if you like the pipe (I personally do), you can always define it in Haskell:
infixl 5 |>
x |> f = f x
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
|> addOne
|> squareIt
|> subtractOne
|> divideByTheNumberYouThoughtOf
Thank you. Really nice.
– Ivan Saracino
yesterday
add a comment |
up vote
1
down vote
Operator & from Data.Function is the exact analog of the F# pipe:
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
& addOne
& squareIt
& subtractOne
& divideByTheNumberYouThoughtOf
Of course, if you like the pipe (I personally do), you can always define it in Haskell:
infixl 5 |>
x |> f = f x
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
|> addOne
|> squareIt
|> subtractOne
|> divideByTheNumberYouThoughtOf
Thank you. Really nice.
– Ivan Saracino
yesterday
add a comment |
up vote
1
down vote
up vote
1
down vote
Operator & from Data.Function is the exact analog of the F# pipe:
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
& addOne
& squareIt
& subtractOne
& divideByTheNumberYouThoughtOf
Of course, if you like the pipe (I personally do), you can always define it in Haskell:
infixl 5 |>
x |> f = f x
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
|> addOne
|> squareIt
|> subtractOne
|> divideByTheNumberYouThoughtOf
Operator & from Data.Function is the exact analog of the F# pipe:
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
& addOne
& squareIt
& subtractOne
& divideByTheNumberYouThoughtOf
Of course, if you like the pipe (I personally do), you can always define it in Haskell:
infixl 5 |>
x |> f = f x
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in numberYonThoughOf
|> addOne
|> squareIt
|> subtractOne
|> divideByTheNumberYouThoughtOf
edited yesterday
answered yesterday
Fyodor Soikin
41.3k56295
41.3k56295
Thank you. Really nice.
– Ivan Saracino
yesterday
add a comment |
Thank you. Really nice.
– Ivan Saracino
yesterday
Thank you. Really nice.
– Ivan Saracino
yesterday
Thank you. Really nice.
– Ivan Saracino
yesterday
add a comment |
up vote
0
down vote
Operator (>>=) from Control.Monad package may be able to imitate |> operator in F# with using Identity from Control.Monad.Identity like:
import Control.Monad.Identity;
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in runIdentity
$ pure numberYonThoughOf
>>= pure . addOne
>>= pure . squareIt
>>= pure . subtractOne
>>= pure . divideByTheNumberYouThoughtOf
Thank you. This is the kind of answer I was looking for. Regards. Ivan
– Ivan Saracino
yesterday
add a comment |
up vote
0
down vote
Operator (>>=) from Control.Monad package may be able to imitate |> operator in F# with using Identity from Control.Monad.Identity like:
import Control.Monad.Identity;
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in runIdentity
$ pure numberYonThoughOf
>>= pure . addOne
>>= pure . squareIt
>>= pure . subtractOne
>>= pure . divideByTheNumberYouThoughtOf
Thank you. This is the kind of answer I was looking for. Regards. Ivan
– Ivan Saracino
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
Operator (>>=) from Control.Monad package may be able to imitate |> operator in F# with using Identity from Control.Monad.Identity like:
import Control.Monad.Identity;
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in runIdentity
$ pure numberYonThoughOf
>>= pure . addOne
>>= pure . squareIt
>>= pure . subtractOne
>>= pure . divideByTheNumberYouThoughtOf
Operator (>>=) from Control.Monad package may be able to imitate |> operator in F# with using Identity from Control.Monad.Identity like:
import Control.Monad.Identity;
thinkOfANumber numberYonThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYonThoughOf
in runIdentity
$ pure numberYonThoughOf
>>= pure . addOne
>>= pure . squareIt
>>= pure . subtractOne
>>= pure . divideByTheNumberYouThoughtOf
answered yesterday
assembly.jc
590211
590211
Thank you. This is the kind of answer I was looking for. Regards. Ivan
– Ivan Saracino
yesterday
add a comment |
Thank you. This is the kind of answer I was looking for. Regards. Ivan
– Ivan Saracino
yesterday
Thank you. This is the kind of answer I was looking for. Regards. Ivan
– Ivan Saracino
yesterday
Thank you. This is the kind of answer I was looking for. Regards. Ivan
– Ivan Saracino
yesterday
add a comment |
up vote
0
down vote
Fyodor Soikin is correct that & corresponds to F#'s |>, but my impression from reading Haskell code is that it's considered more idiomatic to use the $ operator and compose from right to left, like in mathematics:
thinkOfANumber numberYouThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYouThoughOf
in divideByTheNumberYouThoughtOf $ subtractOne $ squareIt $ addOne numberYouThoughOf
add a comment |
up vote
0
down vote
Fyodor Soikin is correct that & corresponds to F#'s |>, but my impression from reading Haskell code is that it's considered more idiomatic to use the $ operator and compose from right to left, like in mathematics:
thinkOfANumber numberYouThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYouThoughOf
in divideByTheNumberYouThoughtOf $ subtractOne $ squareIt $ addOne numberYouThoughOf
add a comment |
up vote
0
down vote
up vote
0
down vote
Fyodor Soikin is correct that & corresponds to F#'s |>, but my impression from reading Haskell code is that it's considered more idiomatic to use the $ operator and compose from right to left, like in mathematics:
thinkOfANumber numberYouThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYouThoughOf
in divideByTheNumberYouThoughtOf $ subtractOne $ squareIt $ addOne numberYouThoughOf
Fyodor Soikin is correct that & corresponds to F#'s |>, but my impression from reading Haskell code is that it's considered more idiomatic to use the $ operator and compose from right to left, like in mathematics:
thinkOfANumber numberYouThoughOf =
let addOne x = x + 1
squareIt x = x * x
subtractOne x = x - 1
divideByTheNumberYouThoughtOf x = x / numberYouThoughOf
in divideByTheNumberYouThoughtOf $ subtractOne $ squareIt $ addOne numberYouThoughOf
answered yesterday
Mark Seemann
181k33322554
181k33322554
add a comment |
add a comment |
Ivan Saracino is a new contributor. Be nice, and check out our Code of Conduct.
Ivan Saracino is a new contributor. Be nice, and check out our Code of Conduct.
Ivan Saracino is a new contributor. Be nice, and check out our Code of Conduct.
Ivan Saracino is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371480%2fscott-wlaschin-thinkofnumber-example%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I mean, you could just write the whole thing in one line
think n x = (a * a - 1) / n - n where a = x + 1– Mateen Ulhaq
yesterday
1
I think this is more appropriate for codereview.stackexchange.com
– hellow
yesterday
2
The F# pipe operator is available as
(>>>)in theControl.Arrowmodule. You could also try using operator sections.– bradrn
yesterday
@bradrn the
>>>operator fromControl.Arrowis actually composition, whereas F# pipe is reverse application.– Fyodor Soikin
yesterday
Thank you for clarifying @Fyodor Soikin - I'm not too familiar with F#. In that case
(&)would indeed be the correct choice as you said below.– bradrn
yesterday