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










share|improve this question









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 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 the Control.Arrow module. You could also try using operator sections.
    – bradrn
    2 hours ago















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










share|improve this question









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 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 the Control.Arrow module. You could also try using operator sections.
    – bradrn
    2 hours ago













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










share|improve this question









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.



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






share|improve this question









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.











share|improve this question









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.









share|improve this question




share|improve this question








edited 3 hours ago









Mateen Ulhaq

11.1k114690




11.1k114690






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 3 hours ago









Ivan Saracino

11




11




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 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 the Control.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 think this is more appropriate for codereview.stackexchange.com
    – hellow
    3 hours ago






  • 2




    The F# pipe operator is available as (>>>) in the Control.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












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





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });






    Ivan Saracino is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    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

























    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





    share|improve this answer

























      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





      share|improve this answer























        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        assembly.jc

        575211




        575211






















            Ivan Saracino is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            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.















             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            'app-layout' is not a known element: how to share Component with different Modules

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            WPF add header to Image with URL pettitions [duplicate]