Archive for March, 2008

Problem #10 Run-length encoding of a list.

Tuesday, March 25th, 2008

[code='ruby'] let pack plist = let rec rec_pack rest packed acc a = match rest with [] -> ( packed @ [acc] ) |x::xs -> if ( x = a ) then ...

Problem #9 Pack consecutive duplicates of list elements into sublists.

Thursday, March 20th, 2008

[code='ruby'] #light let pack plist = let rec rec_pack rest packed acc a = match rest with [] -> ( packed @ [acc] ) |x::xs -> if ( x = a ) then ...

Problem #8 Eliminate consecutive duplicates of list elements.

Wednesday, March 19th, 2008

This problem was a bit harder than usual. A couple of things were learned from doing this problem. Using [] to enclose an a' type while get rid of type mismatch errors. Another development tip is to build the inner recursive function first and then make the wrapper function. [code='ruby'] let compress_list ...

Problem #7

Wednesday, March 12th, 2008

Flatten a nested list structure. This solution builds off the last one in that you need to to recursively match the your result so far and tail recursively add the the rest of the result. [code='ruby'] let rec flatten_list slist = match slist with ...

Problem #6

Tuesday, March 11th, 2008

You can reverse the list and then match it to the original or try to solve it how humans actually check if a string is a palindrome. That is by simultaneously scanning the string from left to right and right to left char by char checking if they match. The ...

Problem #5

Saturday, March 8th, 2008

Similiar solution to problem #4, I used recursion to reverse the list. [code='ruby'] let rec reverse_list l = match l with | [] -> [] | (x::xs) -> (reverse_list xs) @ [x];; myList |> reverse_list |> List.iter (printf "[%d]") ...

Problem #4

Saturday, March 8th, 2008

This also can be done using using one of the built functions but lets try to do this using recursion. [code='ruby'] let rec nr_elements l = match l with | [] -> 0 | x::xs -> 1 + ...

Problem #3

Thursday, March 6th, 2008

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 ...

Problem #2

Monday, March 3rd, 2008

The second problem seems a lot like number 2. [code='ruby'] // 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] ...