Problem #3

March 6, 2008 – 3:24 am

So the problem can be trivial using List.nth so I can go ahead and use that as my solution. But where is the fun in that?

Let’s try and learn something. We are not trying to be efficient, we are trying to learn something.

This will be a good place to start using recursion. The thing with functional programming is that you have to use a keyword to tell the compiler that this function is going to be called within the function itself.

So lets start off with the basic skeleton of a recursive function that iterates through a list.


#light
let rec p03 list =
    match list with
    | y::ys -> p03 ys
    | []    -> []

The “rec” tells the compiler, that this function is recursive. The match list with is used to type match the list. The list is either empty or has some elements.


#light
let rec p03 list k =
    match list with
    | y::ys -> (match k with
                | 1 -> y
                | _ -> if (k > 1) then p03 ys (k - 1)
                       else y)
    | []    -> []

This statement just adds another match statement within the first case. Also note the |_ ->, this tells the compiler for any other values go to this case. Then just a simple if else statement to catch negative numbers and this problem is solved.

Also as a general note, don’t use tabs with #light, it took me a while to figure this out.

Problem #2

March 3, 2008 – 12:02 pm

The second problem seems a lot like number 2.


// Problem 02: Find the last but one element of a list

// last_but_one(X,L) :- X is the last but one element of the list L
//    (element,list) (?,?)

#light
let thisList = [1; 2; 3; 4; 5; 6]
let l = List.length thisList
List.nth thisList (l - 2)
     |> printf "Answer: %d"  

I still haven’t really seen an F# tutorial. I am not bragging, I am apologizing since my solution is no where as refined and as elegant as the solutions that got posted at Frickensweet. I think I am doing myself a bad service by not reading up on F#. So I intend on at least reading up on the tutorials before going on.

Problem #1

February 28, 2008 – 5:03 am

So I begin this long journey by tackling the first problem. It doesn’t seem hard you just need to return the tail of list. It seems like you can just use List.tl? The problem is you can’t. List.tl returns ‘a list but you need ‘a . So another way is to get the size of the list and return the tail by specifying which index to return. “|> ” change functions together so that you pass the result from one function as a parameter to another.


#light
let thisList = [1; 2; 3; 4; 5; 6]
let l = List.length thisList
List.nth thisList (l - 1)
     |> printf "Answer: %d"

Very straight forward.

Ninety Nine problems … AND a bit aint 1

February 27, 2008 – 12:17 am

When I was talking my functional programming class back at my university, I absolutely hated programming in OCaml. Not that I have graduated and started coding on my own, I kind of miss it. Since it’s always fun to learn a new language, I decided to learn F#. I will be tackling 99 problems in order to learn the language. I want to able to say that I have used a lot of programming languages extensively. So far my list includes C, C++, C#, Java, PHP, CSS, HTML, coldfusion, SQL, T-SQL and javascript. For those that don’t think HTML is a language, look up the acrynonm :)

To start off I will learn more about F# at Microsoft website..

F# @ Microsoft Research

Also feel free to leave any solutions that are can be used to solve it in a different way. That way we can mutually learn the language. Or if you like solve it in a different language.

EDIT: My apologizes the reason for these was originally inspired by these guys http://www.frickinsweet.com/99problems/post/Starting-things-off.aspx

func_num_args() and func_get_arg()

December 13, 2007 – 2:29 am

func_num_args() and func_get_arg() are both very useful and powerful function that can be called from user defined functions.
Now these examples are taken from the php.net


<?
function foo()
{
    $numargs = func_num_args();
    echo "Number of arguments: $numargs";
}
foo(1, 2, 3);    // Prints 'Number of arguments: 3'
?>

The example should be pretty intuitive but if not, let me explain. func_num_args() returns the number of variable number of arguments that were passed to a function. Like the in he above example three arguments were passed to function foo.


<?
function foo()
{
     $numargs = func_num_args();
     echo "Number of arguments: $numargs";
     if ($numargs = 2) {
         echo "Second argument is: " . func_get_arg(1) . "n";
     }
}
foo (1, 2, 3);
?>

In this example func_get_arg gets a copy of the “ith” variable number of argument.
Now if you combine these two functions, you can create something like this.


<?
function append_default(&$obj)
{
	$default = func_get_arg(1);
	for($i = 2, $total = func_num_args(); $i < $total; $i++)
		$obj->{func_get_arg($i)} = $default;
}
append_default($some_obj, "0", "age", "height", "weight");
?>

In this example, you can call function on some object and have default values appended to its member variables. In this example the $some_obj will have its age, height, weight, member variables get a default value of “0″. This works because func_get_arg(1) will return a copy of the first variable number of argument. This is important to not because you can’t pass a reference to an object to as variable like above. This is why the append_default explicitly requires a reference to an object. Also notice a good php performance speed up technique in the above code. It is inefficient to have to recompute func_num_args at every loop. Rather it is better practice, to save the result of the function like in the above code.