We were learning more Prolog stuffs today at college and one of the tasks was to add all the even numbers leading up to a number (so for 8 it would be 8,6,4,2 and for 9 it would be the same).
So, my first function took the number and a var for the result and then I wrote two extra functions which had an extra paramter which indicated if it was odd or even.
addEven(N,X):- Z is N mod 2, addEven(N,X,Z).
addEven(N,X,0):- Y is N-2, addEven(Y,X2,0), X is Y + X2.
addEven(N,X,1):- Y is N-1, addEven(Y,X2).
(I think it was something like that I did...)
My lecture went another way, which I didn't note down but he had an 'even' function which basically did what I was doing in the first rule. God knows whos was better as I never got him to look at mine.. I'll grab his code at some point and compare I think...
All in all I'm getting along fine with Prolog, the biggest problem I'm having is having to think in a recursive manner, when C++ coding I tend to favor loops over recursion so when placed into a language with only recursion my brain kinda looks at it funny... it's a matter of practise I guess.
I infact had to bail on a problem in class today because I was too tired, I'll attack it again later (I've had sleep since then) as I refuse to be beaten by a stupid programming problem...
In other news; the assignment for the gfx module is do something in 2D... I've decided on a 2d side scrolling shoot 'em up because it'll allow for plenty of 2D things...
Has to be in Java.
To make up for this I intend on adding many Cat based pictures to it for random head breaking reasons... which should at least amuse me [grin]
Assumning it's any good I'll pimp it here post-results...
Right, I'm due down the pub soon... guess I should get ready to leave..