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
    yesterday








  • 1




    I think this is more appropriate for codereview.stackexchange.com
    – hellow
    yesterday






  • 2




    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










  • 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















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
    yesterday








  • 1




    I think this is more appropriate for codereview.stackexchange.com
    – hellow
    yesterday






  • 2




    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










  • 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













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 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 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 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










  • 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








  • 1




    I think this is more appropriate for codereview.stackexchange.com
    – hellow
    yesterday






  • 2




    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










  • 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












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





share|improve this answer























  • Thank you. Really nice.
    – Ivan Saracino
    yesterday


















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





















  • Thank you. This is the kind of answer I was looking for. Regards. Ivan
    – Ivan Saracino
    yesterday




















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





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

























    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





    share|improve this answer























    • Thank you. Really nice.
      – Ivan Saracino
      yesterday















    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





    share|improve this answer























    • Thank you. Really nice.
      – Ivan Saracino
      yesterday













    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





    share|improve this answer














    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






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    Fyodor Soikin

    41.3k56295




    41.3k56295












    • Thank you. Really nice.
      – Ivan Saracino
      yesterday


















    • Thank you. Really nice.
      – Ivan Saracino
      yesterday
















    Thank you. Really nice.
    – Ivan Saracino
    yesterday




    Thank you. Really nice.
    – Ivan Saracino
    yesterday












    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





















    • Thank you. This is the kind of answer I was looking for. Regards. Ivan
      – Ivan Saracino
      yesterday

















    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





















    • Thank you. This is the kind of answer I was looking for. Regards. Ivan
      – Ivan Saracino
      yesterday















    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 yesterday









    assembly.jc

    590211




    590211












    • 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






    Thank you. This is the kind of answer I was looking for. Regards. Ivan
    – Ivan Saracino
    yesterday












    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





    share|improve this answer

























      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





      share|improve this answer























        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





        share|improve this answer












        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered yesterday









        Mark Seemann

        181k33322554




        181k33322554






















            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

            Feedback on college project

            Futebolista

            Albești (Vaslui)