Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. A little F# for you

A little F# for you

Scheduled Pinned Locked Moved The Lounge
csharpphpwpfcomregex
113 Posts 48 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Stuart Dootson

    Cool - your next task is to learn a) how to apply implicitly recursive functions like fold and map rather than explicit recursion, and b) recognise when they can be applied. To use your example, except using Haskell, 'cause that's what I like: Explicit recursion:

    sum [] = 0
    sum (head:tail) = head + (sum tail)
    

    Differences from F# - recursion and pattern matching don't need to be explicitly stated. Oh, and if you ask Haskell what the type of sum is, it'll reply with (Num t) => [t] -> t - basically saying that sum is now defined for lists of any numeric type...polymorphism at work - uber cool! Implicit recursion:

    sum = foldl (+) 0
    

    A fold combines all elements of a collection using some function - so sum combines all elements of a list using the + operator (Haskell lets you use operator as functions if you surround them with brackets) starting with the accumulating value set to zero. Oh - and even though I've not defined any parameters to this sum definition, it does take one - sum is defined as a partial application of foldl, so it's equivalent to sum nums = foldl (+) 0 nums. Oh - and this definition of sum is defined over all numeric lists as well :-) - the type of foldl is (a -> b -> a) -> a -> [b] -> a - a and b are arbitrary types, (+) has type (Num a) => a -> a -> a and 0 has type (Num t) => t. Combine those types all together and you get the same as the first definition of sum! OK - I'm an FP language dweeb, I'll admit it...

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #95

    Well, I read this and I'm still not sure I get it. Aah - wait a second, with my mathematician head on I get it.

    Deja View - the feeling that you've seen this post before.

    S 1 Reply Last reply
    0
    • J Josh Smith

      I've been studying F# a lot recently and find it really mind-bending.  Tomas Petricek, a fellow CPian, let me sneak preview his series of F# articles and they are very good.  I took one of his examples and modified it a bit.  The following code displays "sum = 6", but how that happens is other-worldly...check it out:

      #light

      let rec sum nums =
        match nums with
        | head::tail -> head + sum(tail)
        | [] -> 0
       
      printf "sum = %i" (sum [1; 2; 3])

      Weird, eh?   F# is coooool. :cool:

      :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

      C Offline
      C Offline
      codemunkeh
      wrote on last edited by
      #96

      Perl made entirely from Regexes and then encrypted in Hex, is still more understandable than that. What's wrong with debug.Print("Sum = 6");


      Ninja (the Nerd)
      Confused? You will be...

      1 Reply Last reply
      0
      • P Pete OHanlon

        Well, I read this and I'm still not sure I get it. Aah - wait a second, with my mathematician head on I get it.

        Deja View - the feeling that you've seen this post before.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #97

        OK -  lets do it in C++... Explicit recursion:

        template<class ListIter>
        std::iterator_traits<ListIter>::value_type sum(ListIter begin, ListIter end)
        {
           if (begin == end) return 0; // == sum [] = 0
           if (begin != end) return (*begin) + sum(begin+1, end); // == sum (head:tail) = head + (sum tail) ... well, kind of
        }
        

        Implicit recursion:

        template<class ListIter>
        std::iterator_traits<ListIter>::value_type sum(ListIter begin, ListIter end)
        {   
           return std::accumulate(begin, end, 0, std::plus<std::iterator_traits<ListIter>::value_type>());
        }
        

        We're representing lists using the pair (ListIter begin, ListIter end) and the set of types for which this compiles is less easy to determine, but that aside it's pretty much the same as the Haskell code, even down to being polymorphic. In C++, std::transform == map and std::accumulate == foldl.

        1 Reply Last reply
        0
        • C Chris Kaiser

          Can you reply to any post without at least a few paragraphs that require reading more than once? Are you in anyway related to the Grand Negus?

          M Offline
          M Offline
          Matthew Faithfull
          wrote on last edited by
          #98

          Yes.

          Nothing is exactly what it seems but everything with seems can be unpicked.

          1 Reply Last reply
          0
          • C Chris Maunder

            That looks like a huge advancement in clarity and code maintainability. (Where's the sarcasm icon when I need it)

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            F Offline
            F Offline
            Fatbuddha 1
            wrote on last edited by
            #99

            Really a nice reply!! :laugh: Cheers

            You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

            1 Reply Last reply
            0
            • J Josh Smith

              I've been studying F# a lot recently and find it really mind-bending.  Tomas Petricek, a fellow CPian, let me sneak preview his series of F# articles and they are very good.  I took one of his examples and modified it a bit.  The following code displays "sum = 6", but how that happens is other-worldly...check it out:

              #light

              let rec sum nums =
                match nums with
                | head::tail -> head + sum(tail)
                | [] -> 0
               
              printf "sum = %i" (sum [1; 2; 3])

              Weird, eh?   F# is coooool. :cool:

              :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

              R Offline
              R Offline
              Rich Leyshon
              wrote on last edited by
              #100

              Superb, just a couple of possible improvements that could be made: 1) Replace all readable characters with further punctuation marks 2) Put a C somewhere in the name of the language and hey presto - mainstream! Rich

              M 1 Reply Last reply
              0
              • J Josh Smith

                I've been studying F# a lot recently and find it really mind-bending.  Tomas Petricek, a fellow CPian, let me sneak preview his series of F# articles and they are very good.  I took one of his examples and modified it a bit.  The following code displays "sum = 6", but how that happens is other-worldly...check it out:

                #light

                let rec sum nums =
                  match nums with
                  | head::tail -> head + sum(tail)
                  | [] -> 0
                 
                printf "sum = %i" (sum [1; 2; 3])

                Weird, eh?   F# is coooool. :cool:

                :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

                R Offline
                R Offline
                redflyingpig
                wrote on last edited by
                #101

                I just don't get the point of yet another functional language before all those 'mainstream' FPs such as Haskell or Scheme become widely accepted by programmers. They have developed over 10 years and are much more mature than this F# thing, which by the look of this tiny program, is not syntactically attractive at all compared with those mentioned above. And I simply don't like their attitudes of trying to 'go mainstream' by the support of .Net while ignoring the efforts and accomplishments already put and gained in the functional programming area.

                1 Reply Last reply
                0
                • J Josh Smith

                  I've been studying F# a lot recently and find it really mind-bending.  Tomas Petricek, a fellow CPian, let me sneak preview his series of F# articles and they are very good.  I took one of his examples and modified it a bit.  The following code displays "sum = 6", but how that happens is other-worldly...check it out:

                  #light

                  let rec sum nums =
                    match nums with
                    | head::tail -> head + sum(tail)
                    | [] -> 0
                   
                  printf "sum = %i" (sum [1; 2; 3])

                  Weird, eh?   F# is coooool. :cool:

                  :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

                  M Offline
                  M Offline
                  MajorTom123
                  wrote on last edited by
                  #102

                  Josh, There are always people to make up a new language to do something, but no one to modify an existing language to do that too. Some things cannot be accomplished in previous languages, but I believe there is alot that can be done, instead of making up a new language, and then the creators of that language have to come up with terms no one understands because the words used are not used the same way in that language. *taking a breath right now* F# looks like APL, and LISP and Haskell, etc... I agree with some of the newer ideas about iterative approaches, but someone HAS to be smart enough to be able to present it clearly and in a maintainable manner in a language. I think that's a summary of all of the posts. No one can comment on the language because you haven't spelled out what each line is accomplishing. If you did that, then you may be able to move people to look into the language further. To me its just another language that is either so specialized we'll never use it or another one of the languages that proclaims itself to be able to kill all other languages. I still use C alot. Supposed to be killed by C++, which was killed by Java which was killed by C# which only runs on one platform with work being partly done on another platform. Ruby is a web language killer, whereas Python was supposed to be, PHP before that, and Perl. The cynic rests his case.

                  1 Reply Last reply
                  0
                  • R Rich Leyshon

                    Superb, just a couple of possible improvements that could be made: 1) Replace all readable characters with further punctuation marks 2) Put a C somewhere in the name of the language and hey presto - mainstream! Rich

                    M Offline
                    M Offline
                    MajorTom123
                    wrote on last edited by
                    #103

                    I second that, except make the Space character a functional part of the language. Why relegate the lonely space to nullability? Who played God and said the space was just a holding character? I make a motion that all spaces be removed and that the space become the "If" in the conditional statement. I further motion that W replace the space and a lowly holding character. Sample code: wwww ;=1w)=)+23 wwww#)=2 Could be written to make it pretty wwww ;w=w1w)w=w)w+w23 wwww#w)w=w2 That says if semicolon equals one then right parenthesis equals right parenthesis plus twenty three else right parenthesis equals two. OHHHH the elegance of it all. It is so clear and maintainable.

                    1 Reply Last reply
                    0
                    • G Gary Wheeler

                      More like waking up the next morning and wondering what species is laying next to you.


                      Software Zen: delete this;

                      M Offline
                      M Offline
                      mfhobbs
                      wrote on last edited by
                      #104

                      Just got func'ed by an alien.

                      1 Reply Last reply
                      0
                      • M Mike Dimmick

                        It's for problems where you indicate what you want performed on every member of a list (map) or how you aggregate the contents of a list (reduce). In theory, because each of the operations is atomic (independent of the other items), in the case of the map operation, or the result of reducing the whole list is the same as reducing distinct subparts of the list then reducing the results of those suboperations, you can farm out subparts of the list to other cores to execute in parallel. Because it's implicit, the environment can scale the algorithm appropriately to the number of installed cores. Still, it's not much that a parallel foreach couldn't do. It's just we're not used to writing our programs as such discrete blobs of functionality, effectively putting half your program out-of-line. And I'm not sure that many of the programs we regularly use would benefit - your data set would have to be pretty big to overcome the cost of the inter-thread procedure calling required to get another core working on part of the problem. I'm not totally sure the MHz myth is as over as it seemed a few years ago. The 45nm generation with high-k dielectric looks like it may have solved or at least alleviated the leakage problems that caused such trouble with overheating when trying to ramp up the clock speeds. The Core 2 Duo E8500 is supposed to clock at 3.16GHz while keeping a Thermal Design Power of 65W (source[^]).


                        DoEvents: Generating unexpected recursion since 1991

                        D Offline
                        D Offline
                        Dr Luiji
                        wrote on last edited by
                        #105

                        It's impossible to express better the right concept, but let me a bit more. F# is a functional programming language and this is its goal. But unfortunatly I think programmers will wanna take less time to deploy the application but they will take more time to thinking with this logic, more time to undestend after few months, more pc power... You can choose when use this or another language thinking in this.

                        Dr.Luiji Trust and you'll be trusted.

                        1 Reply Last reply
                        0
                        • J Josh Smith

                          I've been studying F# a lot recently and find it really mind-bending.  Tomas Petricek, a fellow CPian, let me sneak preview his series of F# articles and they are very good.  I took one of his examples and modified it a bit.  The following code displays "sum = 6", but how that happens is other-worldly...check it out:

                          #light

                          let rec sum nums =
                            match nums with
                            | head::tail -> head + sum(tail)
                            | [] -> 0
                           
                          printf "sum = %i" (sum [1; 2; 3])

                          Weird, eh?   F# is coooool. :cool:

                          :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

                          J Offline
                          J Offline
                          JigPu
                          wrote on last edited by
                          #106

                          Wow that code looks familiar... Almost exactly like the ML code I'm studying in class... Guess I shouldn't be surpised after how similar C# is to Java :D

                          1 Reply Last reply
                          0
                          • J Josh Smith

                            I've been studying F# a lot recently and find it really mind-bending.  Tomas Petricek, a fellow CPian, let me sneak preview his series of F# articles and they are very good.  I took one of his examples and modified it a bit.  The following code displays "sum = 6", but how that happens is other-worldly...check it out:

                            #light

                            let rec sum nums =
                              match nums with
                              | head::tail -> head + sum(tail)
                              | [] -> 0
                             
                            printf "sum = %i" (sum [1; 2; 3])

                            Weird, eh?   F# is coooool. :cool:

                            :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

                            C Offline
                            C Offline
                            ClockMeister
                            wrote on last edited by
                            #107

                            Oh man ... I thought Turbo Pascal was dead! -CB ;)

                            J 1 Reply Last reply
                            0
                            • R Rocky Moore

                              Josh Smith wrote:

                              There's nothing better than freeing your mind a little bit,

                              As long as it does not fall out and get lost in the process :)

                              Rocky <>< Blog Post: MVC for ASP.NET! Tech Blog Post: Cheap Biofuels and Synthetics coming soon? Tech Sites: SilverlightCity.com ~ TheSilverlightDirectory.com ~ TheWPFDirectory.com

                              J Offline
                              J Offline
                              JMOdom
                              wrote on last edited by
                              #108

                              Josh Smith wrote: There's nothing better than freeing your mind a little bit, Rocky Moore wrote: As long as it does not fall out and get lost in the process As a wiseman(?) once about his mind wandering: "Don't worry, its small and feeble and can't get far." ;P:laugh:

                              R 1 Reply Last reply
                              0
                              • C ClockMeister

                                Oh man ... I thought Turbo Pascal was dead! -CB ;)

                                J Offline
                                J Offline
                                JMOdom
                                wrote on last edited by
                                #109

                                Pardon my stupidity, but I've been away from the coding scene for about a year and a half. :confused::doh:Just what is F#, and when did it come out? Thank you. X|

                                C 1 Reply Last reply
                                0
                                • J JMOdom

                                  Josh Smith wrote: There's nothing better than freeing your mind a little bit, Rocky Moore wrote: As long as it does not fall out and get lost in the process As a wiseman(?) once about his mind wandering: "Don't worry, its small and feeble and can't get far." ;P:laugh:

                                  R Offline
                                  R Offline
                                  Rocky Moore
                                  wrote on last edited by
                                  #110

                                  :laugh:

                                  Rocky <>< Blog Post: MVC for ASP.NET! Tech Blog Post: Cheap Biofuels and Synthetics coming soon?

                                  1 Reply Last reply
                                  0
                                  • M martin_hughes

                                    The daft thing is that they could have designed the syntax to be clear and maintainable, but instead went down the path of ghastly and even more ghastly.

                                    Me: Can you see the "up" arrow? User:Errr...ummm....no. Me: Can you see an arrow that points upwards? User: Oh yes, I see it now! -Excerpt from a support call taken by me, 08/31/2007

                                    T Offline
                                    T Offline
                                    Tim Yen
                                    wrote on last edited by
                                    #111

                                    That is soooo true! Why do functional programmers always want bizarre syntax that is not readable? Job security? Tim

                                    1 Reply Last reply
                                    0
                                    • J JMOdom

                                      Pardon my stupidity, but I've been away from the coding scene for about a year and a half. :confused::doh:Just what is F#, and when did it come out? Thank you. X|

                                      C Offline
                                      C Offline
                                      ClockMeister
                                      wrote on last edited by
                                      #112

                                      Don't feel stupid - I only just heard about it myself. I guess it's another Dot-Net language they're coming out with. Not sure why we need yet another one but there you go. -CB :)

                                      1 Reply Last reply
                                      0
                                      • J Josh Smith

                                        I've been studying F# a lot recently and find it really mind-bending.  Tomas Petricek, a fellow CPian, let me sneak preview his series of F# articles and they are very good.  I took one of his examples and modified it a bit.  The following code displays "sum = 6", but how that happens is other-worldly...check it out:

                                        #light

                                        let rec sum nums =
                                          match nums with
                                          | head::tail -> head + sum(tail)
                                          | [] -> 0
                                         
                                        printf "sum = %i" (sum [1; 2; 3])

                                        Weird, eh?   F# is coooool. :cool:

                                        :josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.

                                        G Offline
                                        G Offline
                                        Guillaume Ranslant
                                        wrote on last edited by
                                        #113

                                        This looks extremely similar to CAML/OCAML[^]/SML

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups