Archive for the ‘99 problems’ Category
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
...
Posted in 99 problems, F# | No Comments »
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
...
Posted in 99 problems, F# | 1 Comment »
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 ...
Posted in 99 problems, F# | 3 Comments »
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 ...
Posted in 99 problems, F# | 1 Comment »
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 ...
Posted in 99 problems, F# | 1 Comment »
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]") ...
Posted in 99 problems, F# | No Comments »
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 + ...
Posted in 99 problems, F# | No Comments »
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 ...
Posted in 99 problems, F# | No Comments »
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] ...
Posted in 99 problems, F# | No Comments »
Thursday, February 28th, 2008
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 ...
Posted in 99 problems, F# | 2 Comments »