Posts

Showing posts with the label lisp

Exploring Common Lisp: Project Euler Number 8 in Common Lisp (Neophyte Version)

There isn't really much I can add about this example. The goal is to find the largest product of any consecutive digits given a 1000 digit number. I implemented the first approach that came to mind; I went with a large loop to iterate through the 1000 digits and then an inner loop to extract that digits we are going to use in our calculation. The key built-in lisp functions are "loop" and "reduce". If you are not used to a lisp programming language, a C version of this solution is provided. ;; ;; euler8.lisp (modify to find the "correct" solution) ;; Find the greatest product of five ;; consecutive digits in the 1000-digit number ;; ;; [1] http://www.unixuser.org/~euske/doc/cl/loop.html ( defparameter *large-num* "73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843" ) ( defun find-great-product () "Use reduce to find the product of a list of 5 digits out of the larger 1...

Declarative programming introduction

"Haskell is a very elegant language, and the functional nature of it makes it a beautiful glue language... because the nature of Haskell as a language can make things clearer than many other programming languages" -- Mark C. Chu-Carroll of the ScienceBlogs "Haskell is a declarative language in that the program can be treated as a statement of facts, rather than as a set of commands that are sequenced in time" -- cdsmith Declarative programming is a programming paradigm that encourages that the programming logic describes what the program will do as opposed to how it will do it. Also, you don't define your programming logic in a sequence of steps like you normally would using an imperative style programming. Functional programming is considered declarative programming. With the imperative programming paradigm, one might do the following to reach a desired state. In Java (imperative style): final SavingsAccount savings = new SavingsAccount (); // ...

Doing it wrong, fun with Clojure

Image
I certainly take a liberal approach to programming, some may call it quantity over quality but I like to enjoy everything I do. This may include doing it wrong, but sometimes I like the end result. I am working on a SWT GUI tool and could not figure out a clean way to avoid SWT's illegal thread exceptions. When a user clicked a menu option on the gui, I needed to first take text from an edit box field and then use that data to pass to a long running thread. This is what I ended up with: ;; Define the clojure 'agent', save the state of our search box ( def *search-text-state* (agent nil)) ;; Define an implementation of 'SelectionAdapter/Interface' ;; SelectionAdapter requires one method 'widgetSelected' ;; With display.syncExec, pass an implementation of java.lang.Runnable ;; The thread updates the 'agent' search-text-state text. ;; After we get this text value, then launch another thread 'start-findgrep' ( def findgrep-listener (p...

Simple lisp interpreters in C, Java and Erlang

Image
Overview I have already covered the lisp interpreters in Erlang and Java. I have created another example that is based on C. So, you have three different implementations in three different languages. Also, these interpreters are based on Peter Norvig's JScheme implementation. That is a fourth project that you can compare and contrast. Peter Norvig created JScheme, a Lisp Scheme dialect written in Java. It was created in 1998 and it has since been forked, extended and used quite extensively in other modern projects for the web and elsewhere. The C lisp interpreter is based on the JScheme interpreter. The JScheme code is copyrighted to Peter Norvig This is only a modifications to make our Lisp even lighter than it was originally. Norvig's most recent implementation of JScheme contains 1905 lines of Java code. This implementation contains 2000 lines of C code wrapped in additional Javadoc comments, including implementations of list, hashtable datastructures. There is...

Neophyte Scheme/Lisp Interpreter in Erlang

Image
Overview This is a follow-up to my earlier entry on Lisp in Java; the entry described implementing a Lisp interpreter in Java. This blog posts covers implementing a similar approach using Erlang. In essence, a port of the JSceme interpreter to Erlang. So you can see compare and contrast the differences in Erlang and Java code. Like I always do, I am going to first introduce Erlang with simple examples and then go over the simple Lisp implementation. All of the source is provided at the end of the entry. What is Erlang? Erlang is a general purpose programming language that is great for writing concurrent applications and components. That is accomplished by some of the slimmed-down language features including immutable code, simple message passing primitives, and lightweight processes. It was created by Ericsson in 1987 and later open sourced in 1998. Erlang language syntax is based on Prolog. Why Erlang? I don't know why the software community can't accept another progr...