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.
  • R Rama Krishna Vavilala

    Not very different than prolog or scheme (my favorite language). Prolog:

    sum([X | Y], Z) :- sum(Y, A), Z is A + X.
    sum([], 0).

    Scheme:

    (define sum (lambda (x)
    (if (null? x) 0 (+ (car x) (sum (cdr x))))))

    Co-Author ASP.NET AJAX in Action

    L Offline
    L Offline
    leppie
    wrote on last edited by
    #32

    Rama Krishna Vavilala wrote:

    scheme (my favorite language)

    Look out for IronScheme then coming soon (it was called IronLisp, but I rewrote it due to fundamental flaws and optimistic design). Will be on Codeplex, I have to make the project 'public' by 10 Nov, so look out for it :)

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

    1 Reply Last reply
    0
    • J Josh Smith

      // Use the "lightweight" syntax, meaning whitespace has meaning now.
      #light

      // 'rec' means the function is recursive
      // 'sum' is the function name
      // 'nums' is the list of int which is passed in
      let rec sum nums =
      // 'match' indicates that we're going to perform some pattern matching on the values in 'nums'
        match nums with
      // 'head::tail' means "if it exists, remove the first item in the list
      // '->' means "then do this"
      // 'head + sum(tail)' is the recursive call which continues the adding process
        | head::tail -> head + sum(tail)
      // when the list is empty, return 0 and walk back up the call stack
        | [] -> 0
       
      printf "sum = %i" (sum [1; 2; 3])

      :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
      #33

      Would you believe I actually knew what let rec sums meant! And the recursive call, but I need to do some reading on the matching and removing the head.

      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.

        Steve EcholsS Offline
        Steve EcholsS Offline
        Steve Echols
        wrote on last edited by
        #34

        Actually, looking at your example a little closer and figuring out the syntax, that does look pretty damn cool!


        - S 50 cups of coffee and you know it's on!

        • S
          50 cups of coffee and you know it's on!
          Code, follow, or get out of the way.
        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."

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

          leppie wrote:

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

          Keep in mind, I've only been studying F# for a few days now.  But, my current understanding is that F# uses the x::y syntax for dealing with its "list" type.  You can create a list, or pull one apart, with that :: operator.  AFAIK, writing "head::tail" (or foo::goo) is a way of saying "Give me the first cell in the list, and then also give me the remainder of the list."

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

          L 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
            Matthew Faithfull
            wrote on last edited by
            #36

            Having studied Miranda for far longer than humanely reasonable, nearly 6 months, the example given just looks like a very syntactically ineffecient list comprehension. List comprehensions are fantastically powerful once you've wrapped your head around how to use them. There is a more fundamental problem here though:- Purely functional and declarative languages set out to tell the computer what result you want but provide no way to specify how the computer is to get the result, or what it should do with it. Purely procedural languages tell the computer exactly what to do but it's very hard for a person to determine from reading one what the expected result might be. Both of these approaches have merit but are only properly leveraged when they are 'pure'. As soon as you mix the concepts together you loose the benefits of both without gaining the advantages of either. The worst aspects of C++ for example are the ones that are partially declarative at run time like, There exists a giblet called thing => giblet thing = new giblet;. These areas are where the opaqueness that causes most problems with learning, correctness and performance come in. As F# appears, like 'managed' C++ to be an attempt to mix these approaches, albeit biased towards the functional I guess it will fail just as spectacularly. What is really needed is a pure functional language that manipulates primitives which themsleves are purely procedural components, a kind of functional COM. stl gets a little way and Boost a little further but both are hamstrung by language limitations. We're not there yet :)

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

            C 1 Reply Last reply
            0
            • J Josh Smith

              Douglas Troy wrote:

              You know your a nerd when ...

              Takes one to know one. ;P

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

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #37

              I know you are but what am I... :laugh:

              -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

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

                S Offline
                S Offline
                Shog9 0
                wrote on last edited by
                #38

                Nice. :cool:

                every night, i kneel at the foot of my bed and thank the Great Overseeing Politicians for protecting my freedoms by reducing their number, as if they were deer in a state park. -- Chris Losinger, Online Poker Players?

                1 Reply Last reply
                0
                • Q QuiJohn

                  F# you too.


                  Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency! -Emily Dickinson

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

                  David Kentley wrote:

                  F# you too.

                  hahaha ;P

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

                    M Offline
                    M Offline
                    Marc Clifton
                    wrote on last edited by
                    #40

                    Josh Smith wrote:

                    Weird, eh? F# is coooool.

                    I think we need to explore your childhood, Josh. Marc

                    Thyme In The Country
                    Interacx
                    My Blog

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

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

                      Strangely reminiscent of Lisp as well.

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

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

                        D Offline
                        D Offline
                        Dan Neely
                        wrote on last edited by
                        #42

                        ph'nglui magl'nath cthulhu r'lyeh wagn'nagl fhtagn

                        -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

                        E 1 Reply Last reply
                        0
                        • M Marc Clifton

                          Josh Smith wrote:

                          Weird, eh? F# is coooool.

                          I think we need to explore your childhood, Josh. Marc

                          Thyme In The Country
                          Interacx
                          My Blog

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

                          Marc Clifton wrote:

                          I think we need to explore your childhood, Josh.

                          :laugh:  I was raised by a pack of wolves, and ate peyote for breakfast.

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

                          E 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
                            Judah Gabriel Himango
                            wrote on last edited by
                            #44

                            So, you're declaring a function called sum, and it's recursive (the rec part). It takes a parameter called nums. I see some pattern matching going on, but I start getting lost after that. :) Care to explain? *edit*, nevermind, I see you posted comments in another post that explain it. Thanks.

                            Tech, life, family, faith: Give me a visit. I'm currently blogging about: No, Not I - A poem by Holocaust escapee, chief rabbi, and Messiah-follower Daniel Zion The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

                            J 1 Reply Last reply
                            0
                            • J Judah Gabriel Himango

                              So, you're declaring a function called sum, and it's recursive (the rec part). It takes a parameter called nums. I see some pattern matching going on, but I start getting lost after that. :) Care to explain? *edit*, nevermind, I see you posted comments in another post that explain it. Thanks.

                              Tech, life, family, faith: Give me a visit. I'm currently blogging about: No, Not I - A poem by Holocaust escapee, chief rabbi, and Messiah-follower Daniel Zion The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

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

                              See here[^].

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

                              1 Reply Last reply
                              0
                              • Q QuiJohn

                                F# you too.


                                Faith is a fine invention For gentlemen who see; But microscopes are prudent In an emergency! -Emily Dickinson

                                D Offline
                                D Offline
                                Douglas Troy
                                wrote on last edited by
                                #46

                                Now that would be a good Nerd Shirt.


                                :..::. Douglas H. Troy ::..
                                Bad Astronomy |VCF|wxWidgets|WTL

                                1 Reply Last reply
                                0
                                • J Josh Smith

                                  John Simmons / outlaw programmer wrote:

                                  Looks like a bastardization of dBase2 and interpreted basic.

                                  Something tells me that you won't be an F# "early adopter." :)

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

                                  R Offline
                                  R Offline
                                  realJSOP
                                  wrote on last edited by
                                  #47

                                  If it took me almost 7 years to start coding in .net, you can imagine how excited I am about F#...

                                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                  -----
                                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                                  R B 2 Replies Last reply
                                  0
                                  • J Josh Smith

                                    Marc Clifton wrote:

                                    I think we need to explore your childhood, Josh.

                                    :laugh:  I was raised by a pack of wolves, and ate peyote for breakfast.

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

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

                                    Josh Smith wrote:

                                    I was raised by a pack of wolves

                                    we might be related! ;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 Justin Perez

                                      Ravel H. Joyce wrote:

                                      But no, I won't do anything like that. I promise.

                                      Ok good. Like I said you are a very smart kid, and drugs would only ruin your hopes, dreams, and your desire to be a good, honest person. It will kill your zest for life! You really should remove that Hansen song from your signature though, and stop listenting to that crap. That will also ruin your life!:laugh::laugh::laugh::cool:

                                      I get all the news I need from the weather report - Paul Simon (from "The Only Living Boy in New York")

                                      S Offline
                                      S Offline
                                      soap brain
                                      wrote on last edited by
                                      #49

                                      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

                                      J T 2 Replies Last reply
                                      0
                                      • D Dan Neely

                                        ph'nglui magl'nath cthulhu r'lyeh wagn'nagl fhtagn

                                        -- If you view money as inherently evil, I view it as my duty to assist in making you more virtuous.

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

                                        yes, but the big question is... is He dreaming of F#?

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

                                        D 1 Reply Last reply
                                        0
                                        • P Pete OHanlon

                                          Strangely reminiscent of Lisp as well.

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

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

                                          everything old is new again....

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