|
Friday, 19 June 2009 08:50 |
|
Lately, I have been interested in the Scala programming language. It is actually quite nice, and seems to really work with me and not against me. I really like the way the functional aspects are combined with imperative, as it makes it so much easier to think about things when my mind is not in a pure-functional mindset. Here's one little snippet that I've been working on to learn some Scala, another sieve of eratosthenes. It will work if pasted into the scala interpreter, and then just call the primes function. def primes_aux(currentList:List[Int], stopNumber:Int) : List[Int] = { if(currentList == Nil) Nil else { val H = currentList.head if (H <= stopNumber) H :: primes_aux(currentList.tail.remove(num => (num % H) == 0), stopNumber) else currentList } } def primes(maxNumber:Int) : List[Int] = { primes_aux((2 to maxNumber).toList, Math.sqrt(maxNumber)) } I tried really hard to shrink the code from the erlang version, and I was successful. It is much more succint. I will probably find some better functions that can help me get rid of or at least simplify the logic. I have a feeling this can be done in a few short lines of code. |
|
Tuesday, 09 June 2009 22:03 |
|
So, you may be wondering, "I would like to get something nice for Kekoa, but don't know what to get him!" Well, today I have your answer! I have created a list of books on Amazon that I would love to read, and would be so happy to receive as gifts. You can view my list, and have the books shipped directly to me. I added the widget in a convenient location on my site(look on the right) so that it will be easy for you to see what books are left on the list. If you're in the mood to just do something nice and would love to help me out by getting me books that will increase my knowledge and skills, I would appreciate the support. That is the best present I could ask for! |
|
Wednesday, 22 April 2009 11:03 |
|
Playing with iPhone stuff and Objective-C has been very interesting to say the least. I am to the point where I think I like it but don't really know why. Here are some initial thoughts, after a couple months of use... Developers have to be really good at knowingWhatMethodTheyAreWantingToCall, as the methods are ridiculously wordy. I say this now, but it is slowly rubbing off on me. I don't know who developed in objective-c prior to code completion, because it would be impossible without constantly searching the documentation. There is good documentation though, once you figure out what it all means. I like the message passing idea, it adds some dynamic flexibility to an otherwise inflexible language(C). I do like the built in reference counting and memory management system(retain and release). You have to do it yourself in C++ or find a set of classes that does it for you. Interface Builder was a challenge to figure out. The program itself was easy to use, but figuring out how all the connections are to be made, and how to connect and instantiate things was a little bit of a pain at first. But now I'm pretty comfortable with creating my own .nibs and using them. XCode is a beast. It's not really my taste, but I'm learning to use it. Unfortunately, it is no Eclipse, more like Visual Studio. It took a little while to get used to how things work, I've still got questions, but generally things are working smoothly now. |
|
Thursday, 09 April 2009 13:56 |
|
So I was a little disappointed by my last effort, it just wasn't elegant enough. I'm probably going to venture to list comprehensions, but haven't gone there yet. I have a cleaned up version of the last one, it is nicer, and makes more sense(and fixes a bug- 5 points for you if you can find it). -module(sieve). -export([primes/1]).
%% Removes the multiples of a number from a list remove_multiples(_, []) -> []; remove_multiples(N, [H|T]) when (H rem N == 0) -> remove_multiples(N, T); remove_multiples(N, [H|T]) -> [H|remove_multiples(N, T)].
%% Generate primes by creating a list of numbers from 0 to N. %% Then recurse through the list, eliminating multiples off the tail at each recursion. primes(N) -> primes(lists:seq(2,N), math:sqrt(N)). primes([H|T], StopNumber) when H = [H|primes(remove_multiples(H, T), StopNumber)]; %% Does this blow your mind? primes(L,_)-> L.
|
|
Saturday, 04 April 2009 20:53 |
|
This is my first real attempt at a semi-useful program in Erlang. It is the Sieve of Eratosthenes, which is a prime number generator. The main function is primes() which accepts the number that you would like to count up to. Luckly, Erlang is a Functional language, so I was able to do some fun things. I had to modify the algorithm, as the original algorithm uses arrays, and arrays aren't really used in Erlang. I suppose this is probably due to the functional roots that Erlang has. Scheme in practice tends to stay away from vectors and uses lists more. I decided to use lists in Erlang, and was able to do some nice recursion, as is normal with Scheme. -module(sieve). -export([primes/1]).
%% Removes the multiples of a number from a list remove_multiples(_, []) -> []; remove_multiples(N, [H|T]) when (H rem N == 0) -> remove_multiples(N, T); remove_multiples(N, [H|T]) -> [H|remove_multiples(N, T)].
%% Generate primes by creating a list of numbers from 0 to N. %% Then recurse through the list, eliminating multiples at each recursion. primes(N) -> primes(N, lists:seq(2,N)). primes(N, [H|T]) -> StopNumber = math:sqrt(N), if H < StopNumber [H|primes(N,remove_multiples(H, T))]; %% Does this blow your mind? true -> [H|T] end.
I'm not experienced with Erlang yet, so my apologies if I have bad Erlang style. I wanted to put N < math:sqrt(N) in the guard for the function, but apparantly, you can't put function results in guards, so I had to store it in the StopNumber variable, and use an if statement. |
|
<< Start < Prev 1 2 3 4 5 6 7 Next > End >>
Page 1 of 7
|