Functional programming (specifically Microsoft F#)-VBForums
Results 1 to 12 of 12

Thread: Functional programming (specifically Microsoft F#)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    582

    Functional programming (specifically Microsoft F#)

    Hi,

    I 've been experimenting with functional programming, Microsoft F# to be specific.

    After several hours of trying I finally succeeded in making a small application called "FTextBin" that does more than demonstrate a F# function.

    It extracts "human-readable" strings from binary files much like the Linux/Unix strings command. (I wrote something similar in vb6 and vb.net, search the forum for "TextBin".)

    To use it:
    1. Load and compile it with Microsoft Visual Studio.
    2. Start a command line based shell. (Cmd.exe/Power Shell.)
    3. Run the executable with the binary file to be searched as a command line argument.
    4. It should now dump a list of strings to the standard output.

    If an exception occurs during run-time, it will simply display: "Error: "exception message".

    Normally, I would post something like this in the code bank, but I don't think there's one for F#.

    Any way, who else here has experimented and/or has experience with functional programming?
    Last edited by Peter Swinkels; Mar 8th, 2017 at 01:32 PM. Reason: Code now fully written in a functional style.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    6,513

    Re: Functional programming (specifically Microsoft F#)

    It does belong in CodeBank really but you're right, there's no natural home for it. I could move it to CodeBank - Other for you if you'd like.
    You can depend upon the Americans to do the right thing. But only after they have exhausted every other possibility - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    582

    Re: Functional programming (specifically Microsoft F#)

    Quote Originally Posted by FunkyDexter View Post
    It does belong in CodeBank really but you're right, there's no natural home for it. I could move it to CodeBank - Other for you if you'd like.
    Yes, if you could move it there, I would appreciate it.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,485

    Re: Functional programming (specifically Microsoft F#)

    I see you are using F# but you are looping and that kind of flies in the face of a functional language.

    From what I understand some kind of recursion should take the place of that with the function returning the next spot until some end case is met.

    I'm no expert in this - just my two cents!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    30,830

    Re: Functional programming (specifically Microsoft F#)

    Snuck in and made the move ahead of Funky.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    582

    Re: Functional programming (specifically Microsoft F#)

    Quote Originally Posted by szlamany View Post
    I see you are using F# but you are looping and that kind of flies in the face of a functional language.

    From what I understand some kind of recursion should take the place of that with the function returning the next spot until some end case is met.

    I'm no expert in this - just my two cents!
    Thanks for your comments.

    As far as I understand it F# isn't 100% functional, and for now I don't mind if some traces of imperative style coding creep in my F# code. Trust me, figuring out how to do what I wanted in F# was pretty hard considering I originally learned programming with QBasic (and now mostly use VB.Net.) As I understand it, VB.Net supports some degree of functional programming, but both are mostly imperative. What I mean is that I'm already learning by being forced to think ~75% functional when using F#. If any one has any ideas on how to substitute the loop in my code...
    Last edited by Peter Swinkels; Feb 23rd, 2016 at 04:43 AM.

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,485

    Re: Functional programming (specifically Microsoft F#)

    The reason I commented was that I am very familiar with JavaScript used in large applications and I've heard that you could use JavaScript in a "functional manner".

    I do not try to do that with JavaScript myself.

    So I downloaded your app to see what you might be doing.

    I'd be interested to see what others might offer also!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,485

    Re: Functional programming (specifically Microsoft F#)


    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    582

    Re: Functional programming (specifically Microsoft F#)

    Quote Originally Posted by szlamany View Post
    Hi, that looks like a useful link. Thanks.

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    582

    Re: Functional programming (specifically Microsoft F#)

    Quote Originally Posted by Peter Swinkels View Post
    Hi, that looks like a useful link. Thanks.
    Well, it looks like I managed to replace my loops with recursive functions...
    Code:
    //This module's imports and settings.
    open System.IO
    
    //This procedure is executed when this program is started.
    [<EntryPoint>]
    let main argv = 
       try      
          let rec DisplayList (items:List<string>) = 
             printf "%s\n" items.Head
             if items.Length = 1
             then items
             else DisplayList(items.Tail)
          let mutable Texts = ""
          let rec LoopThroughList (items:List<char>) = 
             Texts <- Texts + 
                if items.Head >= ' ' && items.Head <= '~' then items.Head.ToString()
                elif Texts.EndsWith('\u0000'.ToString()) then ""
                else '\u0000'.ToString()
             if items.Length = 1
             then items
             else LoopThroughList(items.Tail)
    
          File.ReadAllBytes(argv.[0]) |> Array.toList |> List.map char |> LoopThroughList |> ignore
          Texts.Split('\u0000') |> Array.toList |> List.distinct |> DisplayList |> ignore
       with
          | exceptionO -> printf("Error: \"%s\"\n") exceptionO.Message
       0
    What I don't like about my recursive functions is that in the case of "LoopThroughList" I haven't yet found a way to get it to return a useful return value when it has reached the last list item. I could assign it to the "Texts" variable instead dumping it to "ignore".

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,485

    Re: Functional programming (specifically Microsoft F#)

    Could you push info into an array or is that against the "grain" of functional?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2003
    Posts
    582

    Re: Functional programming (specifically Microsoft F#)

    After a long while I decided to revisit this project. The program is now written in a fully functional style, that is no mutable values or loops.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.