Problem #6

Now we're going to work on something that is (just slightly) more challenging than what we've encountered so far.

"Find out whether a list is a palindrome."

Sounds easy enough. First off we're going to create a simple method to compare two lists and print if they are equals or not. We would never really do something like this but for the sake of example it works out.

let printListsEqual (listy : List<char>) =

     if listy.Equals(List.rev listy) then

          printfn "It's a palindrome!"

     else

          printfn "Not a palindrome!"

Notice that we're actually defining the parameters as a list of characters (using generics) instead of using the type inference of the F# compiler. We would get a compile error if we did not infer the type and tried to use the equals method.

Next we're going to create two lists. We want the first one to be a palindrome -- where the second is just to prove that our function is working.

let listForward = ['r'; 'a'; 'c'; 'e'; 'c'; 'a'; 'r']

let listGarbage = ['g'; 'a'; 'r'; 'b'; 'a'; 'g'; 'e']

Finally, we're going to run the palindrome thru the function we created above with the reverse of itself as the second parameter... then we're going to run the same method except instead of the reversed list we're going to pass in the garbage list.

printListsEqual listForward

print_newline()

printListsEqual listGarbage

 The results are just as intended -- the result of running the function the first time is 'It's a palindrome', while the second is 'Not a palindrome!' Sweet we're finally on to some slightly more complicated code! The full code is listed below:

    1 #light

    2 

    3 let printListsEqual (listy : List<char>) =

    4      if listy.Equals(List.rev listy) then

    5           printfn "It's a palindrome!"

    6      else

    7           printfn "Not a palindrome!"

    8 

    9 

   10 let listForward = ['r'; 'a'; 'c'; 'e'; 'c'; 'a'; 'r']

   11 let listGarbage = ['g'; 'a'; 'r'; 'b'; 'a'; 'g'; 'e']

  kick it on DotNetKicks.com


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , ,
Categories: F#

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Problem #5

Again this is not too difficult -- After a little while, however, we'll be back into the more challenging stuff.

"Reverse a list."

    1 #light

    2 

    3 let listerine = [1 .. 40] |> List.rev |> List.iter (printfn "%d")

 Basically everything looks familiar until we get to the List.iter statement. In case it's not obvious, the iter statement traverses thru each element of the list -- so really all we're doing is reversing the list we've created and printing each element. 

kick it on DotNetKicks.com  


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , ,
Categories: F#

2 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Problem #4

We've already done this in Problem #2 but for the sake of answering all the problems we'll do it again :) 

"Find the number of elements of a list."

Using the built in list functions and the pipeline operator we come up with the answer pretty easily. 

    1 #light

    2 

    3 let listerine = [1 .. 40]  |> List.length |> printfn "List length: %d"

 

There are not really any new concepts in this solution -- on to problem 5! As always, please feel free to post your solution if it is any different / more elegant -- we're using this excerise a way to better learn the language. 

kick it on DotNetKicks.com  


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: ,
Categories: F#

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Problem #3

Building on what we have already the next couple should be a breeze. Problem #3 is as follows:

 "Find the K'th element of a list."

We can basically simplify problem two a little bit and get the answer for three.

    1 #light

    2 

    3 let listerine k = [1 .. 40]  |> fun x -> List.nth x k

    4 

    5 printfn "Answer: %d" (listerine 27)

This statement should look very similar to problem 2 except for two distinct differences...

  1. We are passing a parameter k to the function
  2. We are no longer calculating the second to last character but rather finding the element k. 

 Pretty straight forward but this will fail if k is out of bounds.

Microsoft.FSharp.Core.InvalidArgumentException: nth

>    at Microsoft.FSharp.Collections.ListModule.nth[T](List`1 l, Int32 n)
   at <StartupCode$FSI_0018>.FSI_0018._main()
stopped due to error

We'll look at how to get around that at a later time :)

kick it on DotNetKicks.com  


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: ,
Categories: F#

2 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Problem #1

Alright, on to the code. The first of the 99 problems goes as follows:

 "Find the last element of a list."

 Sounds easy enough -- we'll start by creating a list, reversing it and printing the first element of the reversed list. Make sure your Tabs for F# are set to 5 spaces instead of a tab since F# looks at whitespace as part of determining what is what (tools->options->text editor->F#->Tabs).

    1 #light

    2 

    3 let myList =

    4      [1; 2; 3; 4]

    5      |> List.rev

    6      |> List.hd

    7      |> printf "Answer: %d"

Which gives us the result "Answer: 4" -- What did we do here to get this result? 

  1. First off, we created a list; this is pretty easy in F#. We could have used either this terminology or the :: append technique.
  2. To understand the next part of this statement, we first need to realize what exactly the |> (pipeline) operator is doing. To quote fsharp.it, "This operator allows [us] to chain functions, passing the output of one of them to the next one." Great, so in effect what the List.rev statement is doing is reversing the list [1; 2; 3; 4].
  3. Now we're going to grab the head element of the first list and print the result.

Please lemme know what you think -- and if you have a better solution, by all means... Trying to get my object oriented brain wrapped around this functional programming :)

  kick it on DotNetKicks.com


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags: , ,
Categories:

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed

Starting things off...

My brother and I are starting this site basically as a way to learn F# by working through the 99 Problems site. I realize that a lot of people are working thru ProjectEuler but after seeing someone implement a few of the 99 problems in Ruby, I thought it may be a good idea as well. Eventually we will be listing other F# resources as well but for now, we'll focus just on the 99 problems.

Please be sure to leave any solutions you may have -- especially if there is a better way of implementing something (trying to fit my object oriented mind around the functional techniques). For more information on F# basics, please check out the following links: 



Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Categories:

1 Comments
Actions: E-mail | Permalink | Comment RSSRSS comment feed