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.
Think of a number: f#
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
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.
Think of a number: f#
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
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
3 hours ago
I think this is more appropriate for codereview.stackexchange.com
– hellow
3 hours ago
2
The F# pipe operator is available as(>>>)
in theControl.Arrow
module. You could also try using operator sections.
– bradrn
2 hours ago
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.
Think of a number: f#
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
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.
Think of a number: f#
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
New contributor
edited 3 hours ago
Mateen Ulhaq
11.1k114690
11.1k114690
New contributor
asked 3 hours ago
Ivan Saracino
11
11
New contributor
New contributor
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
3 hours ago
I think this is more appropriate for codereview.stackexchange.com
– hellow
3 hours ago
2
The F# pipe operator is available as(>>>)
in theControl.Arrow
module. You could also try using operator sections.
– bradrn
2 hours ago
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
3 hours ago
I think this is more appropriate for codereview.stackexchange.com
– hellow
3 hours ago
2
The F# pipe operator is available as(>>>)
in theControl.Arrow
module. You could also try using operator sections.
– bradrn
2 hours ago
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
3 hours ago
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
3 hours ago
I think this is more appropriate for codereview.stackexchange.com
– hellow
3 hours ago
I think this is more appropriate for codereview.stackexchange.com
– hellow
3 hours ago
2
2
The F# pipe operator is available as
(>>>)
in the Control.Arrow
module. You could also try using operator sections.– bradrn
2 hours ago
The F# pipe operator is available as
(>>>)
in the Control.Arrow
module. You could also try using operator sections.– bradrn
2 hours ago
add a comment |
1 Answer
1
active
oldest
votes
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
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 1 hour ago
assembly.jc
575211
575211
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
3 hours ago
I think this is more appropriate for codereview.stackexchange.com
– hellow
3 hours ago
2
The F# pipe operator is available as
(>>>)
in theControl.Arrow
module. You could also try using operator sections.– bradrn
2 hours ago