99 Problems in F# (1-10): 10 down, 89 to go

About a month ago, my brother and I started this site with the intention of going through Dr. Werner Hett's 99 Problems after being inspired by someone implementing them in Ruby. Since we were new to the language, we figured it would be a great way for us to gain a better understanding of the F# language; And it has been. Listed below are the first ten problems out of ninety-nine, so we still have a way to go. . . 

These may not be the "best" implementations of each solution; I'm sure these aren't the "best" solutions. Actually, if you have a better solution; I implore you to post it in the comments so I can edit the post for future viewers to observe your acuity. ;) 

(click the links for the problems to directed to more detailed posts. . . )
 

Problem #1: "Find the last element of a list" (detailed view)

    1 #light

    2 

    3 let myList =

    4      [1; 2; 3; 4]

    5      |> List.rev

    6      |> List.hd

    7      |> printf "Answer: %d" 

 

Problem #2: "Find the last but one element of a list" (detailed view)

    1 #light

    2 

    3 let listerine = [1 .. 40]

    4 let prob2 = List.nth listerine (List.length listerine - 2) |> printfn "Answer: %d"

 

Problem #3: "Find the K'th element of a list" (detailed view)

    1 #light

    2 

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

    4 

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

 

Problem #4: "Find the number of elements of a list" (detailed view)

    1 #light

    2 

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

 

Problem #5: "Reverse a list" (detailed view) 

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

 

Problem #6: "Find out whether a list is a palindrome" (detailed view)

let printListsEqual (listy : List<char>) =

     if listy.Equals(List.rev listy) then

          printfn "It's a palindrome!"

     else

          printfn "Not a palindrome!"

 

Problem #7: "Flatten a nested list structure" (recursively) (detailed view)

    1 #light

    2 

    3 let someList = [[1;2;3];[4;5];[6;7];[8]]

    4 let rec listFixer listy =

    5             if List.nonempty listy then

    6                 let top = listy |> List.hd

    7                 let tail = listy |> List.tl

    8                 top @ (listFixer tail)

    9             else

   10                 []

 

Problem #8: "Eliminate consecutive duplicates of list elements" (detailed view)

    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

 

Problem #9: "Pack consecutive duplicates list elements into sublists" (detailed view)

    1 #light

    2 

    3 let oSnap = [1;1;1;2;2;2;2;2;2;3;3;3;3;3;3;4;]

    4 

    5 let prob9 listegosaurus =

    6     let rec packer listy tempy head tail =   

    7         match tail with

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

    9                     packer listy (tempy @ [head]) h t

   10                   else

   11                     packer (listy @ [tempy @ [head]] ) [] h t

   12         | []   -> listy @ [[head]]

   13 

   14     match listegosaurus with

   15     | h::t   -> packer [] [] h t

   16     | []    -> [listegosaurus]

 

Problem #10: "Run-length encoding of a list" (detailed view)

    1 #light

    2 

    3 let oSnap = [1;1;1;2;2;2;2;2;2;3;3;3;3;3;3;4;]

    4 

    5 let prob10 listupendous =

    6     let rec packer listy tempy head tail =   

    7         match tail with

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

    9                     packer listy (tempy @ [head]) h t

   10                   else

   11                     packer (listy @ [(List.length (tempy @ [head]), head)]) [] h t

   12         | []        -> listy @ [1 , head]

   13 

   14     match listupendous with

   15     | h::t   -> packer [] [] h t

   16     | []    -> [1, (listupendous |> List.hd)]

 That's it so far. If you have any implementations of any of the problems, please toss them in the comments and I'll be sure to put them in the body. 

kick it on DotNetKicks.com  


Currently rated 5.0 by 2 people

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

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

Related posts

Comments

March 29. 2008 06:36

David

Enjoyed the posts so far. You've inspired me to go through the same exercise for the purpose of learning Python Smile
davesquared.blogspot.com/.../...h-python-1-10.html

David

December 23. 2008 10:58

Jeff

I am new to F# as well (about 3 days), but how about this for problem 8:

//Problem 8 (**) Eliminate consecutive duplicates of list elements.

let rec listDropDupsInSequence = function
| [] -> []
| x::y::xt when x = y -> listDropDupsInSequence ([x] @ xt)
| x::xt -> [x] @ listDropDupsInSequence xt

// Execute

System.Console.WriteLine(
listDropDupsInSequence [0;1;1;2;3;4;4;4;4;4;5;6;6;6;7;7;8]
|> any_to_string
)

Jeff

December 23. 2008 11:01

Jeff

//Problem 7 (**) Flatten a nested list structure.

let rec listFlatten = function
| [] -> []
| head::tail -> head @ listFlatten tail

//Execute
System.Console.WriteLine(listFlatten [[0;1];[2;3];[4;5];[6;7]] |> any_to_string)

Jeff

December 23. 2008 11:02

Jeff

//Problems 1 : Find the last element of a list.
let l = [1..10]
let listNthRev list n = List.nth list (list.Length - n)

System.Console.WriteLine(listNthRev l 1)

//Problems 2 : Find the last but one element of a list.
System.Console.WriteLine(listNthRev l 2)

//Problem 3 : Find the K'th element of a list.
System.Console.WriteLine(l.Item 8)

//Problem 4 : Find the number of elements of a list.
System.Console.WriteLine(l.Length)

//Problem 5 : Reverse a list.
System.Console.WriteLine((List.rev l) |> any_to_string)

Jeff