Another Project Euler problem, and a List.fold example

October 15, 2011

Project Euler Problem #6

Part two in what I hope will be a series on solving some of the Project Euler problems just to learn F# (part 1 here).

I skipped down to #6 next, and was able to whip out a few lines of F# pretty quickly to get the answer. Since I was just re-using List.sum and List.map, there wasn’t much new here.

   1:  let numbers = [1..100]
   2:  let squares = numbers |> List.map (fun x -> x*x)
   3:  let sumsquares = List.sum squares
   4:  let sums = List.sum numbers
   5:  let squaresums = sums*sums
   6:  let total = squaresums - sumsquares

Then after a short chat with Richard Minerich, whose blog post on getting started gave me the idea to work on the Project Euler problems to begin with, I decided to condense this a bit. I was able to pretty quickly rewrite lines 2 and 3, and 3 and 4 into:

   1:  let sumsqaures = numbers |> List.map (fun x -> x*x)|> List.sum
   2:  let squaresums = numbers |> List.sum |> (fun x -> x*x)

Around this point, I found Dustin Campbell’s #6 solution, which helped me realize the teensiest (but ridiculously major) thing that you can replace:

   1:  (fun x -> x*x)

with a new square function:

   1:  let square x = x*x

and just call that, instead. Also, there’s no reason not to condense the subtraction to one line – except maybe readability :p – so I end up with:

   1:  let numbers = [1..100]
   2:  let square x = x*x
   3:  let total = (numbers |> List.sum |> square)-(numbers |> List.map square |> List.sum)

Fun! I decided to stop there, and not continue to what ultimately would have been pretty close to his solution (check it out, it’s delightful) because that felt somehow like accidental cheating. :)

List.fold Example

Since #6 was a little easy, I thought I’d add a small thing – I was flipping through Chris Smith’s Programming F# this afternoon, and saw that both he and Chris Ammerman (in the comments last night) provided the same example: using List.fold to take an array of strings into a single comma-delimited string (must be a people-named-Chris-thing to come up with that example.. *shrug*). You can see Chris Smith’s code from the bookhere. Thanks for helping me make sense of that one, folks. :)

Project Euler problems with F#

October 10, 2011

Since I’ve just – JUST – started learning F#, I thought I would post my solutions to the first couple problems on Project Euler:

Problem #1: Add all the natural numbers below 1000 which are multiples of 3 or 5.

   1:  let all = [1 .. 999]
   2:  let filtered = all |> List.filter (fun x -> x%3=0 || x%5=0)
   3:  let count = List.sum filtered

So, first we declare a list of all natural numbers (basically, positive integers, since it doesn’t matter here if 0 is included or not) less than 1000. Then, we take that list and filter it for everything that’s divisible by 3 or 5. And finally, sum the list. Pretty easy. Now that I’ve done it. :)

Problem #2: Find the sum of the even-valued terms of the Fibonacci sequence which are less than 4 million.

   1:  let fibSeq = Seq.unfold (fun (a,b) -> Some( a+b, (b, a+b) ) ) (0,1)
   2:  let fibList = fibSeq |> Seq.takeWhile (fun x -> x<4000000 ) |> Seq.toList
   3:  let filterList = List.filter (fun x -> x%2=0) fibList
   4:  let fibSum = List.sum filterList

By far the hardest part about this problem (for me) was figuring out how to create the Fibonacci sequence. This whole unfolding thing is *totally* new. Basically, you’re starting with (a=0,b=1) and applying (a+b, (b, a+b)) for each new element. Once we have the generating function defined, we send that to takeWhile to only grab values less than 4 million, and put those into a new list. Next, we filter the new list, for only even values. Finally, sum those up.

I would *love* feedback on the code. Or how have you done it? I only went back a couple pages, but I didn’t see a single other F# solution in the forum for those two questions!

Also, a (probably very basic) question: I’ve seen several examples where people use List.fold to sum things. Since List.sum exists, would you ever do that IRL? When *would* you use List.fold? I haven’t been able to quite wrap my head around it. Thanks!

Laid Off Camp Phoenix, Recap.

August 13, 2009

My mind has been buzzing for days with aftereffects of Laid Off Camp Phoenix. I’m thrilled that I was able to be a part of it, and I cannot wait for the next one!

So, let’s step back. First: my history & background: I was laid off in December. I was actually laid off via voicemail while I was on vacation in another country. At the time, I was so burnt out that my only thoughts were, “zomg, sweet! I don’t have to stop being in vacation mode!!” I danced around the house and took the dogs to the dog park. Just hung out and did whatever I felt like for a couple weeks. At that point, a contract had fallen into my lap, so I started working on that. Fast forward to February, and I heard about three different friends being laid off in one week. A couple of us chatted, and we decided to bring Laid Off Camp to Phoenix.

It started slowly (very slowly). Ideas for sessions trickled in & ideas for speakers; we chose a date, lined up gangplank for the venue & set up Facebook, Eventbrite, Meetup and twitter accounts/pages.Then, just as things started to come together, everything needed to change. :) There was so much interest in helping, I started to get more speakers, more signups… I wanted more space, more rooms, more tracks. I spoke to several venues, started pricing things, wondered where on earth a sponsor would fall from the sky to pay for it all. And then, Derek Neighbors and Gangplank saved the day! & hooking me up with the Town of Gilbert, AZ. In a matter of hours, I had a new (fully sponsored) venue, a second food sponsor and a ton of help. I’m extremely grateful to them for *everything*.

I ended up with five rooms. A lunch/networking room; a large room for talks; a smaller room for talks; a room for panels; and one for impromptu sessions. I wanted to keep the unconference/-Camp idea alive, but I wasn’t sure how a mostly non-techie crowd would take to it. So, for the most part, I scheduled the sessions, but left the one room open. I know a couple sessions popped up there, so I’m glad that was utilized.

Overall, I think Laid Off Camp was a success. Enough so, that I was talked into doing another one that night, in 6 months-ish. So, c’mon speakers! Start ramping up your sessions!! Honestly, I’m still processing some of the feedback. I had one woman tell me that, for her, it was “life-changing.” That will stick with me probably forever. Beyond that I think my favorite comment was @jillinski‘s tweet about “visions of reinvented me dancing in my head.” That so exactly and perfectly summed up my intent. A lot of people seemed to come away honestly inspired; & for that, I thank the speakers & panelists. I’m just glad I could gather everyone there to make it happen. :)

Thanks again to:

Followup networking: Susan Baier has created a list of everyone’s twitter handle who was there. Check her blog post to connect! And I created a LinkedIn group for everyone.

Thanks again for an extremely successful Laid Off Camp Phoenix! And I hope to see you all next time!

Laid Off Camp – Phoenix!

May 29, 2009

I first heard about LaidOffCamp when it was being organized in San Francisco — twitter is good like that, word gets out quickly. I recall thinking, “What a great idea! Someone should organize one in Phoenix!” Well, when three of my friends were laid off all the same week in February, we decided that the time had come to host a LaidOffCamp Phoenix ourselves! So we are. :)

Background

LaidOffCamps are ad-hoc gatherings of unemployed and nontraditionally employed people (including freelancers, entrepreneurs and startups) who want to share ideas and learn from each other. They feature an open, participatory discussion forum designed to educate, empower, and connect community members. The various presentations, workshops, and discussions focus on topics that may include: building your personal brand, transitioning to a new industry, legal & accounting demands of launching a new business, finding affordable health insurance, alternative working spaces, alternative income sources, and how to become a freelancer.

Details

Date: Saturday, August 8th

Location: Gangplank
325 E. Elliott Rd
Chandler, AZ 85225

Tickets: GET FREE TICKETS!
You must have a ticket!

The Rules of LaidOffCamp

  • 1st Rule: You do talk about LaidOffCamp.
  • 2nd Rule: You do blog about LaidOffCamp.
  • 3rd Rule: Anyone with something to contribute or with the desire to learn is welcome and invited to join.
  • 4th Rule: No pre-scheduled presentations and no tourists – everyone participates!
  • 5th Rule: If you want to present, you must write your topic and name in a presentation slot.
  • 6th Rule: As many presentations at a time as facilities allow for.
  • 7th Rule: All sponsors, no matter how large their contribution is, shall be treated equally and afforded the same “benefits”
  • 8th Rule: There will be no screening or vetting of presenters – anyone can speak!

Adopted from Tantek Çelik’s The Rules of BarCamp.

 

For even more details, check The Wiki.

 
Powered by Wordpress and MySQL. Theme by Shlomi Noach, openark.org