lisp

The lisp-way to solve Fibonnaci

I wanted to try and learn lisp, but I very quickly gave up. I figured I'd try again. I'm looking at Problem 2 on Project Euler - finding the sum of all the even Fibbonacci numbers under 4 Million. I wrote the following code which works, but is all kinds of ugly. Chief among them is the fact that it's so slow - because it's doing naiv...

Common Lisp: Working with &rest parameters

Can anyone tell me how to work with the parameters stored in the value specified by &rest. I've read around a lot and it seems as if authors only know how to list all the parameters as so. (defun test (a &rest b) b) This is nice to see, but not really that useful. The best I've found so far is to use first, second, and so on to get ...

Fixing Lisp Syntax

Being a newbie to Lisp I'm wondering if the Lisp syntax could be "fixed"? Some people say the syntax in Lisp is one of its biggest strengths. I don't quite understand this. Isn't it possible to replace "obvious" parentheses with a combination of white spaces, new lines and indenting? Just like in Python? It looks to me like parenthe...

Lisp evaluation of let statements

I am writing a Scheme interpreter, and I am faced with a valid let statement such as: ;; should print 7 (let ((a 4) (b 3)) (let ((a (* a a)) (b (* b b))) (+ a b) (- a b))) My interpreter implements only a purely functional subset of Scheme, so there will be no side effects such as set!. In a purely functio...

elisp functions as parameters and as return value

I have the following code (defun avg-damp(f) #'(lambda(x) (/ (+ (funcall f x) x) 2.0))) A call (funcall (avg-damp #'(lambda(v) (* v v))) 10) returns 55.0 (the correct value) in SBCL but crashes with the following stack in emacs lisp Debugger entered--Lisp error: (void-variable f) (funcall f x) (+ (funcall f x) x) (/ (...

How can I tell if a list has a third item?

I have a function that takes a list that either has two or three elements. ;; expecting either ((a b c) d) or ((a b c) d e) (define (has-third-item ls) (if (null? (caddr ls)) false true) ) But this code fails with mcar: expects argument of type <mutable-pair>; given () on the (null? (caddr ls)) expr...

Can you return nothing from a function in Scheme?

I'm writing a scheme interpreter, and in the case of an if statement such as: (if (< 1 0) 'true) Any interpreter I've tried just returns a new prompt. But when I coded this, I had an if for whether there was an alternative expression. What can I return in the if such that nothing gets printed? (if (has-alternative if-expr) (eval (a...

Using ASDF to start Hunchentoot

I'm working on a web app using Hunchentoot (on SBCL and Linux), and usually I just run it from Emacs (SLIME), but for deployment I want something that's easier to automate. So I'm trying to figure out ASDF, because that seems to be what everybody's using these days. myapp.asd: (in-package #:asdf) (defsystem :myapp :name "my app" :...

How do I make a Java class immutable in Clojure?

I'd like to wrap java's PriorityQueue class in clojure for use in another part of my program. What I'm trying to figure out is if there is any way to do this in a lispy manner and make the priority queue immutable. Are there any good ways to do this, or am I just going to be better off using the PriorityQueue as a mutable data structur...

SBCL and LangUtils

Has anyone got langutils working with sbcl? ...

What is the Scheme function to find an element in a list?

I have a list of elements '(a b c) and I want to find if (true or false) x is in it, where x can be 'a or 'd, for instance. Is there a built in function for this? ...

Help me convert my Lisp code to a C-based syntax

If you know LISP, could you please change this into C-based sytnax for me (ie. C/C++/C#/Java ..etc)? (let ((g (* 2 (or (gethash word good) 0))) (b (or (gethash word bad) 0))) (unless (< (+ g b) 5) (max .01 (min .99 (float (/ (min 1 (/ b nbad)) (+ (min 1 (/ g ngood)) ...

Coping with, and minimizing, memory usage in Common Lisp (SBCL)

I have a VPS with not very much memory (256Mb) which I am trying to use for Common Lisp development with SBCL+Hunchentoot to write some simple web-apps. A large amount of memory appears to be getting used without doing anything particularly complex, and after a while of serving pages it runs out of memory and either goes crazy using all...

Loading libraries in Dr Scheme

I am working through SICP using Dr Scheme. How do I load external libraries in Dr Scheme? Say I want to use Math library then how do I ask Dr Scheme to load the particular library? I tried with the following: (require (lib "math.ss")) Got the following error: reference to undefined identifier: require I have chosen R5RS as the langua...

How can I apply a new emacs C style to reformat all my source files?

I'd like to re-format all my source files using the Google formatting function for emacs: google-c-style.el (see here). How can I apply this function to all my source files at once, so that they are all formatted and indented correctly according to the Google style? ...

Clojure keyword arguments

In Common Lisp you can do this: (defun foo (bar &key baz quux) (list bar baz quux)) (foo 1 :quux 3 :baz 2) ; => (1 2 3) Clojure doesn't have keyword arguments. One alternative is this: (defn foo [bar {:keys [baz quux]}] (list bar baz quux)) (foo 1 {:quux 3 :baz 2}) ; => (1 2 3) That's too many nested brackets to have to typ...

Is a functional language a good choice for a Flight Simulator? How about Lisp?

I have been doing object-oriented programming for a few years now, and I have not done much functional programming. I have an interest in flight simulators, and am curious about the functional programming aspect of Lisp. Flight simulators or any other real world simulator makes sense to me in an object-oriented paradigm. Here are my qu...

Is it possible to deploy a Common Lisp (or other dialect) desktop application for several platforms?

I would like to develop a graphical application in Common Lisp or other Lisp dialect that could be deployed in Mac, Windows and Linux as a way of improving my knowledge of this language. Ideally: would compile the code would use a common graphical library wouldn't need installation of the runtime environment. I would like to make a l...

How to enforce maximum line length in Emacs?

In Emacs, how can I enforce a maximum line length of, say, 80 characters? I want it to insert proper line breaks in my code, much like fill-paragraph for text, if possible, with the correct insertion of the second part of the line. Here a little example: LongNameType<PrettyLong, AlsoLong> doSomethingWithLongFunctionName(int a, int b); ...

Class introspection in Common Lisp

Java's java.lang.Class class has a getDeclaredFields method which will return all the fields in a given class. Is there something similar for Common Lisp? I came across some helpful functions such as describe, inspect and symbol-plist after reading trying out the instructions in Successful Lisp, Chapter 10 (http://www.psg.com/~dlamkins/s...