A little F# for you
-
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.
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." -
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.
You know your a nerd when ...
Josh Smith wrote:
Weird, eh? F# is coooool.
:..::. Douglas H. Troy ::..
Bad Astronomy |VCF|wxWidgets|WTL -
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."leppie wrote:
Here is a Scheme example:
Wow, Scheme makes F# look "normal." :wtf:
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
-
You know your a nerd when ...
Josh Smith wrote:
Weird, eh? F# is coooool.
:..::. Douglas H. Troy ::..
Bad Astronomy |VCF|wxWidgets|WTLDouglas 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.
-
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.
I get it, but I couldn't do it. Match says if nums is null, return 0, otherwise return head + sum(tail). But: What does the "head::tail ->" mean?
-
leppie wrote:
Here is a Scheme example:
Wow, Scheme makes F# look "normal." :wtf:
:josh: My WPF Blog[^] Without a strive for perfection I would be terribly bored.
But once you learn it :) Having a brace matching editor is rather essential for any function more than a few lines.
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." -
I get it, but I couldn't do it. Match says if nums is null, return 0, otherwise return head + sum(tail). But: What does the "head::tail ->" mean?
Thats the pattern matcher I assume :)
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." -
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."Now that hurts! :~
-
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.
Ah, once more in the key of G.
Deja View - the feeling that you've seen this post before.
-
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.
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
-
Thats the pattern matcher I assume :)
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."OK, F# isn't going to be a one night stand.
-
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.
-
I get it, but I couldn't do it. Match says if nums is null, return 0, otherwise return head + sum(tail). But: What does the "head::tail ->" mean?
// 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.
-
:-O:-O:-O:-O:-O:-O:-O:-O:-O:-O:-O:-O:-O:-O I'm not sure if it's possible to blush or be embarrassed to death, but I shall try! Uh, ahem! Anyway...pretending that was never uttered by me...:~ Umm....you've really...struck me dumb. I'm quite literally speechless. Not a word, a sentiment, an opinion, or even a sardonic quip. :cool: But no, I won't do anything like that. I promise. :rose:
Mmmbop, ba duba dop Ba du bop, ba duba dop Ba du bop, ba duba dop Ba du, yeah Mmmbop, ba duba dop Ba du bop, Ba du dop Ba du bop, Ba du dop Ba du, yeah
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")
-
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.
I have a flute that plays in F#, but the best sound comes from the low C.
_________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)
-
// 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.
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." -
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
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." -
// 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.
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.
-
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.
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!
-
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."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.