Euler Problem 1

November 3rd, 2008

I’ve decided to do a series of posts with my solutions to the Euler problems. They’ll be tagged with “euler”, and once I figure out how I’ll stop them from showing up on the main page.

This is the first Euler Problem
  1. (define sum
  2.   (lambda (x) (apply + x)))
  3. (define product
  4.   (lambda x (apply * x)))
  5. (define sum-1-to-n
  6.   (lambda (x) (* (/ 1 2) x (+ x 1))))
  7. (define (1+ x) (+ x 1))
  8. (define (-1+ x) (- x 1))
  9.  
  10. (let
  11.     ((smalllist '(3 5 6 9 10 12 15))
  12.      (num15s (quotient 1000 15)))
  13.   "(* num15s 15) -> 990, so we add the last few by hand"
  14.   (+ (* 15
  15.         (length smalllist)
  16.         (sum-1-to-n (-1+ num15s)))
  17.      (* num15s (sum smalllist))
  18.      993 995 996 999))

Continuing project, programming contest, perl

November 2nd, 2008

The project I’m working on I briefly mentioned last post is a deliberately simple roguelike set in a branching dungeon. It’s 60-70% done at current, and I’ll say no more until I at least get to final bugfixing.
I went to the ACM programming contest, in which our school placed very average. I had more fun that I expected from the day before and less than from the week before. I had lots of delicious free food. I’d do it again, overall, and be more interested in doing so if I knew my team well ahead of time.
I started learning perl, which looks exactly as disgusting as everyone says it is. It seems useful. It has very odd array/hash behavior which relate to its origin in string processing. An array of three arrays is just the contents of each of those three. To make the kind of thing I want, you need to make explicit reference variables. Interestingly, the fact that you can only pass one array (because they’re flattened) to a function means that everything is passed by value to functions. To my knowledge this is also true of the assignment operator. Since perl as I mentioned supports references, this is to my liking… the array flattening less so.

Regular expressions are as always kickass, and I understand perl’s engine is the best to be found. I really need to get a Linux system soon so I can do fun things like use the “grep” command or have very large text consoles.

Python and a new project

October 22nd, 2008

This past month I’ve been learning python. I wanted to see what all the hype was about. As it turns out, it’s actually justified. I don’t feel like python is a fundamental improvement over imperative langauges like C++ , php, and Java that I already know, but it’s more streamlined. The language design is obviously excellent. The code is easy to read. For example, the if statement is

if b==0:
    print "Divide by zero
else:
    print a/b

you get used to the forced formatting. It’s definitely worth it to have shorter, nicer-looking code. Debugging: b=0 is not a legal conditional. You don’t have to declare variables, but you do have to assign somethint to them before passing them to a function, etc. The compiler will usually catch something like:

abc=12
print fibbonacci(abbc)

The exception to this rule is attributes of objects. Objects can have attributes added dynamically, so it doesn’t work. My pet peeves so far: Python has some behavior toward global variables I still can’t figure out. Lambda expressions are not lambda functions, but they’re similar enough I expected them to be. They raised by expectations and dashed them. You can pass functions as objects, make them at runtime, etc. You just have to name them. So I don’t like the keyword “lambda”, although I admit I have no better alternative. Similarly, there’s a while..else and for…else that do not do what I would expect. Default functions for arguments and default values for objects are only initialized once. For instance,

function cache(item, array = []):
array.append(item)
print array
cache(1) --> [1]
cache(2) --> [1, 2]

This probably slightly optimizes the resulting program but it’s counterintuitive. My last gripe, and one that seems a language standard (for instance this annoyed me about Java), is the following: assignment is inconsistent. Numbers and get copied by value, objects and collections get a reference copied. I’d like to see designers go with a deep copy, shallow copy, or reference for EVERYTHING sometime, so I don’t have to check types when I’m doing an assignment, particularly in a dynamically-typed language like Python. However, deep and shallow copies are available as probably-can-do-it functions in a standard python module. Overall, Python seems like an amazingly relaxing language to program in (from someone who gets stressed over debugging easily). It also seems like it has decent libraries from what I’ve seen so far–I’ll have to examine that more when it comes up (my current project being a roguelike, I only need to print colored ASCII characters). But, it also doesn’t seem fundamentally different from C++, etc. So if you’re into the whole imperative, optionally object-oriented paradigm I recommend it, but I probably won’t be sticking with it for my own use.

The Blue Programming Language

September 29th, 2008

Recently I’ve been searching for a new programming language, becoming discontent with the prospects of Scheme. That’s not to say I don’t like Scheme–just that no one I know is willing to use or read anything I write in it. So, I’m currently looking at a few langauges, and along the way I stopped by a little scripting language Eric Lechak wrote called Blue. It’s got a core object oriented system I like alright, although I feel it has a few too many things left over from C. It doesn’t feel elegant to me (coming most recently from Scheme, what would?), but it executes quickly.

However, this object system I was talking about is pretty neat, because you can arbitrarily give any object an attribute, like a color (string) or a diameter (floating point). Methods are just function attributes. I got into a discussion (read: polite row) about types with Mr. Lechak, and found out that if we add something he names prototyping, we have the object-oriented system of my dreams. Prototyping allows (in C++ terms) every object to act as the “prototype” for a new class.

Now, it makes some things awkward, but since I don’t use too much object-oriented design (no multiple inheritance, for instance) it’s good by me. Mr. Lechak tells me it will slow down execution speed (particularly for his main concern, multiple processors), which I will have to take his word for, knowing nothing about compiler design. Being a firm believer in ease-of-programming over speed of execution, I care not a whit, and continue to be enchanted by the simplicity of prototyping.

Be sure to tune in next week, when I hate prototyping and demonstrate my new favorite language, <foo>.