-
Feb 21st, 2016, 01:58 PM
#1
Thread Starter
Frenzied Member
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 02:32 PM.
Reason: Code now fully written in a functional style.
-
Feb 22nd, 2016, 03:36 AM
#2
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.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
-
Feb 22nd, 2016, 10:48 AM
#3
Thread Starter
Frenzied Member
Re: Functional programming (specifically Microsoft F#)
Originally Posted by FunkyDexter
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.
-
Feb 22nd, 2016, 11:29 AM
#4
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!
-
Feb 22nd, 2016, 11:47 AM
#5
Re: Functional programming (specifically Microsoft F#)
Snuck in and made the move ahead of Funky.
My usual boring signature: Nothing
-
Feb 22nd, 2016, 12:52 PM
#6
Thread Starter
Frenzied Member
Re: Functional programming (specifically Microsoft F#)
Originally Posted by szlamany
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 05:43 AM.
-
Feb 22nd, 2016, 05:46 PM
#7
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!
-
Feb 22nd, 2016, 05:50 PM
#8
Re: Functional programming (specifically Microsoft F#)
-
Feb 23rd, 2016, 05:44 AM
#9
Thread Starter
Frenzied Member
Re: Functional programming (specifically Microsoft F#)
Originally Posted by szlamany
Hi, that looks like a useful link. Thanks.
-
Feb 23rd, 2016, 11:14 AM
#10
Thread Starter
Frenzied Member
Re: Functional programming (specifically Microsoft F#)
Originally Posted by Peter Swinkels
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".
-
Feb 23rd, 2016, 11:30 AM
#11
Re: Functional programming (specifically Microsoft F#)
Could you push info into an array or is that against the "grain" of functional?
-
Feb 12th, 2017, 11:51 AM
#12
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|