Problem #8

"Eliminate consecutive duplicates of list elements."

My solution:

    1 #light

    2 

    3 let problem7 lister =

    4     let rec checker head tail =

    5         match tail with

    6         | h::t       -> if head = h then

    7                            checker h t

    8                         else

    9                            [head] @ checker h t

   10         | []        -> [head]          

   11     match lister with

   12     | h::t   -> checker h t

   13     | []    -> lister

   14 

   15 let megaList = [1;1;1;1;1;2;2;2;2;3;3;3;3;3;3;4;4;4;4;5;5;5;6;6;7;7;7

Alright. What this is going to do is take the ridiculously huge megaList and make it prettier. 

First off, the list that's passed into prob7 is matched with either 'being a list' (h::t // which is just matching the list against it's elements) OR being an empty list ([]).

If the passed-in tail (tail) is not empty, we're going to see if the head of the passed-in tail (h) is the same as the passed-in head (head).

If they're equal, we're going to run the checker with the head of the passed-in tail (h) and the tail of the passed-in tail (t) to check the next set of values. So we're just tossing out all of the values in the list that have the same value except the last one.

If they're not equal, we're appending the results of the checker function with the head of the passed-in tail (h) and the tail of the passed-in tail (t) to check the next set of values. 

And If the passed-in tail (tail) is empty ([]), we have to return the passed in head, to make sure we're getting a return value when the passed in list only contains one element. 

I'd just like to thank Jesus DeLaTorre's beautiful code that helped me ultimately understand the power of match statements for this solution and avoid some really ugly looking code. ;) 

kick it on DotNetKicks.com  


Currently rated 5.0 by 1 people

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

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

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#

4 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#

4 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#

4 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#

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