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

    J Offline
    J Offline
    Josh Smith
    wrote on last edited by
    #71

    Thanks Mike.  That's a very informative post.  I think I'll read it again now. :)

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

    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

      E Offline
      E Offline
      El Corazon
      wrote on last edited by
      #72

      Chris Maunder wrote:

      Where's the sarcasm icon when I need it

      If you can't do anything about it, you expect us to help you find one? ;P

      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

      1 Reply Last reply
      0
      • J Josh Smith

        Pete O'Hanlon wrote:

        Grown bored with WPF now have you?

        Argh! Don't say that!!  I, too, feel that I "must remain" pidgeon-holed into WPF.  But I don't want to feel trapped like that.  I am a bit tired of WPF at the moment, considering that I've studied nothing else for the past two years!  Plus, part of my agenda[^] is to see if/how F# and WPF can work together.

        Pete O'Hanlon wrote:

        No challenges left there?

        Yeah right.  I've only scratched the surface.

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

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

        I'm going to be studying TIDDLY WINQS[^].

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

        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;

          B Offline
          B Offline
          Brady Kelly
          wrote on last edited by
          #74

          It's been so long since that's happened.

          J 1 Reply Last reply
          0
          • S soap brain

            Justin Perez wrote:

            stop listenting to that crap

            :suss: ... ... ... ...I almost didn't change it, you know...'Mmmbop' is fun! You're killing my zest for life.

            "Who wants waffles? We got a new album out...it's called 10,000 Days. Buy it so I can afford waffles." -Maynard James Keenan

            T Offline
            T Offline
            ToddHileHoffer
            wrote on last edited by
            #75

            Ravel H. Joyce wrote:

            "Who wants waffles? We got a new album out...it's called 10,000 Days. Buy it so I can afford waffles." -Maynard James Keenan

            Tool is the best. Maynard is a genius.

            I didn't get any requirements for the signature

            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

              C Offline
              C Offline
              code frog 0
              wrote on last edited by
              #76

              That is just what I was thinking. What a load of crap for code. Why not just write a function that takes a list and it sums everything in between? Sure would look cleaner. Gosh anything would look cleaner compared to that. I've seen pascal code that did more with less.

              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.

                B Offline
                B Offline
                Bob Nadler
                wrote on last edited by
                #77

                Wow... The F# code just gave me a lisp flashback (car (cdr (cons ... ))))))))) :wtf: Functional but unreadable. Don't worry though, I'm better now.

                Bob on Medical Device Software [^]

                1 Reply Last reply
                0
                • L leppie

                  Thats simply a map/reduce pattern, also called folding. Here is a Scheme example:

                  (define (fold func accum lst)
                  (if (null? lst)
                  accum
                  (fold func (func accum (car lst)) (cdr lst))))

                  (define (sum . lst) (fold + 0 lst))

                  (display "sum = ")
                  (display (sum 1 2 3)) ; prints 6 (0 + 1 + 2 + 3)
                  (newline)

                  (define (product . lst) (fold * 1 lst))
                  (display "product = ")
                  (display (product 1 2 3)) ; prints 6 (1 * 1 * 2 * 3)

                  xacc.ide
                  The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

                  P Offline
                  P Offline
                  Patrick Etc
                  wrote on last edited by
                  #78

                  Wow. That takes me back. Haven't seen that stuff since college.


                  "If you think of yourselves as helpless and ineffectual, it is certain that you will create a despotic government to be your master. The wise despot, therefore, maintains among his subjects a popular sense that they are helpless and ineffectual." - Frank Herbert

                  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

                    A Offline
                    A Offline
                    Al Beback
                    wrote on last edited by
                    #79

                    (Where's the sarcasm icon when I need it) This is all we got to work with: :rolleyes:


                    Christianity: The belief that a cosmic Jewish zombie who was his own father can make you live forever if you symbolically eat his flesh and telepathically tell him you accept him as your master, so he can remove and evil force from your soul that is present in humanity because a rib-woman was convinced by a talking snake to eat from a magical tree... yeah, makes perfect sense.

                    1 Reply Last reply
                    0
                    • B Brady Kelly

                      It's been so long since that's happened.

                      J Offline
                      J Offline
                      Josh Smith
                      wrote on last edited by
                      #80

                      Brady Kelly wrote:

                      It's been so long since that's happened.

                      What are we talkin', days?  weeks?

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

                      B 1 Reply Last reply
                      0
                      • J Josh Smith

                        Steve Echols wrote:

                        Whoa!

                        My sentiments exactly!  The hardest part about learning F#, for me, is the fact that it has both new syntax and new concepts (well, new for me anyways).  I must say, though, that I haven't had this much geeky fun in a long time!  There's nothing better than freeing your mind a little bit, and thinking about things from a totally different perspective. :-D

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

                        R Offline
                        R Offline
                        Roger Wright
                        wrote on last edited by
                        #81

                        It's vaguely familiar, though... Prolog, maybe?

                        "A Journey of a Thousand Rest Stops Begins with a Single Movement"

                        1 Reply Last reply
                        0
                        • J Josh Smith

                          Brady Kelly wrote:

                          It's been so long since that's happened.

                          What are we talkin', days?  weeks?

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

                          B Offline
                          B Offline
                          Brady Kelly
                          wrote on last edited by
                          #82

                          I exagerate, but just the other night I woke up with my face in a chapter on the ASP.NET 2.0 request handling chain.

                          1 Reply Last reply
                          0
                          • J Josh Smith

                            Rama Krishna Vavilala wrote:

                            of WPF or F#?

                            I meant WPF.  I don't even know where the surface of F# is yet! :)

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

                            R Offline
                            R Offline
                            Rama Krishna Vavilala
                            wrote on last edited by
                            #83

                            You are being very modest.:) Was it because I mentioned Karen contacted you for WPF in Action review or where you in the game before.:)

                            Co-Author ASP.NET AJAX in Action

                            J 1 Reply Last reply
                            0
                            • R Rama Krishna Vavilala

                              You are being very modest.:) Was it because I mentioned Karen contacted you for WPF in Action review or where you in the game before.:)

                              Co-Author ASP.NET AJAX in Action

                              J Offline
                              J Offline
                              Josh Smith
                              wrote on last edited by
                              #84

                              Rama Krishna Vavilala wrote:

                              Was it because I mentioned Karen contacted you for WPF in Action review or where you in the game before.

                              Oh, was it you who got them in touch with me?  Thanks a lot! :-D

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

                              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.

                                S Offline
                                S Offline
                                sacoskun
                                wrote on last edited by
                                #85

                                Huh, I hope that this code is being written automatically by dragging Sum and Numbers controls from toolbox and dropping them on a page:) Moreover, 5th line looks like a smiley |[]->0 .

                                J 1 Reply Last reply
                                0
                                • S sacoskun

                                  Huh, I hope that this code is being written automatically by dragging Sum and Numbers controls from toolbox and dropping them on a page:) Moreover, 5th line looks like a smiley |[]->0 .

                                  J Offline
                                  J Offline
                                  Josh Smith
                                  wrote on last edited by
                                  #86

                                  sacoskun wrote:

                                  Huh, I hope that this code is being written automatically by dragging Sum and Numbers controls from toolbox and dropping them on a page

                                  Yeah, and that little dog in the Windows Explorer Search screen barks at you while you drag stuff around. :rolleyes:

                                  sacoskun wrote:

                                  Moreover, 5th line looks like a smiley |[]->0

                                  Wow, good catch!!

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

                                  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.

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

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

                                    J P 2 Replies Last reply
                                    0
                                    • 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...

                                      J Offline
                                      J Offline
                                      Josh Smith
                                      wrote on last edited by
                                      #88

                                      Stuart Dootson wrote:

                                      OK - I'm an FP language dweeb, I'll admit it...

                                      Wow, but you're one knowledgable FP language dweeb!  Thanks for sharing that with me.  I'll have to study what you wrote, to make sure it sticks.

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

                                      1 Reply Last reply
                                      0
                                      • L leppie

                                        Josh Smith wrote:

                                        'head::tail' means "if it exists, remove the first item in the list

                                        Wouldnt that just destructure your input list into 'head' and 'tail' variable? Perhaps the '::' has special meaning? :confused:

                                        xacc.ide
                                        The rule of three: "The first time you notice something that might repeat, don't generalize it. The second time the situation occurs, develop in a similar fashion -- possibly even copy/paste -- but don't generalize yet. On the third time, look to generalize the approach."

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

                                        leppie wrote:

                                        Wouldnt that just destructure your input list into 'head' and 'tail' variable?

                                        When used on the left hand side of a pattern match, yes. When used on the right hand side, it's just a cons operator (i.e. adds an element to the front of a list). So, blah head::tail -> head::tail leaves the list unchanged.

                                        1 Reply Last reply
                                        0
                                        • J Josh Smith

                                          Chris Maunder wrote:

                                          That looks like a huge advancement in clarity and code maintainability.

                                          Hahaha.  Yeah right.  I get the feeling that F# isn't going to become a "mainstream" .NET language anytime soon.  It's out there: far, far out there.  It has virtues different from clarity and code maintainability.  I'm just a newbie so don't quote me, but supposedly using F# as a functional programming language allows you to more easily write code which can be parallelized across multiple processors or cores.  I'm interested to see how to do that, because I think that's an important aspect of modern software design.

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

                                          M Offline
                                          M Offline
                                          Member 96
                                          wrote on last edited by
                                          #90

                                          Interesting in that it's a .net language, presumably you could take the assembly from f# and turn it into c# code. I'd like to see what reflector would make out of that stuff.


                                          Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt

                                          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