Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

## #1 2012-12-20 06:15:16

anonimnystefy
Real Member

Offline

### Lisp

To start a little Lisp discussion, here are two functions I wrote:

#### Code:

```(defun fibonacci (n)
(labels ((fib (n f)
(if (or (zerop n) (eq n -1))
(car f)
(fib (- n 1)  (cons (+ (first f) (second f)) f ))
)))
(fib (- n 2) (1 1)))
)

(defun ints (n)
(labels ((f (n acc)
(if (zerop n)
acc
(f (- n 1) (cons n acc))
)))
(f n nil))
)```

The first one is for calculating the nth Fibonacci number, and the second for generating a list of the first n positive integers.

You can test them by entering something like:

#### Code:

`(mapcar #'fibonacci (ints 20))`

Last edited by anonimnystefy (2012-12-20 07:40:07)

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #2 2012-12-20 20:37:36

anonimnystefy
Real Member

Offline

### Re: Lisp

Anyone have an idea on what I could do next?

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #3 2012-12-20 21:07:25

bob bundy
Moderator

Offline

### Re: Lisp

Can you do graphics with it?

If so, it is the ideal language for fractals.

Bob

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #4 2012-12-20 21:12:41

anonimnystefy
Real Member

Offline

### Re: Lisp

Not yet. I could learn that, though...

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #5 2012-12-20 21:37:35

bob bundy
Moderator

Offline

### Re: Lisp

It should do the recursion really well and to quite a depth.

If it works, post up the pictures.

Bob

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #6 2012-12-20 21:42:43

anonimnystefy
Real Member

Offline

### Re: Lisp

How do I even start any work on graphics with Lisp?

Yes, it quite a capability for recursion.

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #7 2012-12-20 22:02:52

bob bundy
Moderator

Offline

### Re: Lisp

Our friend Mr Google will give you plenty of hits.  Lisp programmers seem to be pretty generous with their code/ideas.

But you'll need to add what implementation you are using.

Bob

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #8 2012-12-20 22:04:24

anonimnystefy
Real Member

Offline

### Re: Lisp

I tried Googling, but none of the things I tried worked...

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #9 2012-12-20 23:28:22

bob bundy
Moderator

Offline

### Re: Lisp

What version of lisp?  What did you try?  (Just one at this stage)

Bob

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #10 2012-12-21 06:20:17

anonimnystefy
Real Member

Offline

### Re: Lisp

I use SBCL for CLisp.

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #11 2012-12-21 11:33:33

anonimnystefy
Real Member

Offline

### Re: Lisp

Hi Bob

I have a package called "vecto". I am searching for instructions on using it...

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #12 2012-12-21 14:52:57

bob bundy
Moderator

Offline

### Re: Lisp

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #13 2012-12-21 20:02:41

anonimnystefy
Real Member

Offline

### Re: Lisp

Hi Bob

That is the page I found! I will try what I can today...

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #14 2012-12-22 10:23:52

anonimnystefy
Real Member

Offline

### Re: Lisp

Hi Bob

I have wrotten this code: http://pastebin.com/Je8PQMfX which should produce some kind of grayyscale Mandelbrot set, but it doesn't...

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #15 2012-12-22 23:31:20

bob bundy
Moderator

Offline

### Re: Lisp

So what does it produce?

Have you worked up to this with simple stuff like draw a line, plot a pixel, shade an area etc.

Bob

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #16 2012-12-22 23:37:22

anonimnystefy
Real Member

Offline

### Re: Lisp

Hi Bob

I edited my code a little bit:

#### Code:

```(defun mandelbrot (size filename)
(let ((img (make-8-bit-gray-image size size)))
(declare (type 8-bit-gray-image img))
(loop for i from 1 to (1- size) do
(loop for j from 1 to (1- size) do ;these two loops go through all pixels of the image
(labels ((mb-set (x y nit) ;nit is the number of the iteration
(if (or (> (+ (* x x) (* y y)) 2) (> nit 1000)) ;this bit of code checks if the modulo of z is >2
(setf (pixel img i j) (round(/ 255.0 (1+ nit)))) ;if it is, this bit sets the current pixel to the appropriate shade of grey
(mb-set (+ (- (* x x) (* y y)) (/ (- j (round (/ size 2.0))) 50.0)) (+ (* 2 x y) (/ (- j (round (/ size 2.0))) 50.0)) (1+ nit)) ;if not, this bit sends new information into the next iteration
)
))
(mb-set 0.0 0.0 0)
)
)
)
(write-jpeg-file filename img)
)
)```

but it produces the picture below...

Uploaded Images

Last edited by anonimnystefy (2012-12-23 01:09:52)

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #17 2012-12-23 00:31:27

bob bundy
Moderator

Offline

### Re: Lisp

Ok.  A common mistake which you can easily fix:

There are no comments / remarks / annotation to go with this code.

Bob

LATER EDIT

When you recompute x and y you need to store the new x temporarily whilst computing the new y, else you use the wrong x.

Recommend you don't call mb-set again until you have reset values in some extra lines of code, rather than packing all into a single line.

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #18 2012-12-23 01:05:55

anonimnystefy
Real Member

Offline

### Re: Lisp

Hi Bob

The variable x is never reassigned once in the function...

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #19 2012-12-23 01:07:55

bob bundy
Moderator

Offline

### Re: Lisp

Sorry.  Was just about to re-edit that load of rubbish.

how about

(mb-set (+ (- (* x x) (* y y)) (/ (- j (round (/ size 2.0))) 50.0)) (+ (* 2 x y) (/ (- j (round (/ size 2.0))) 50.0)) (1+ nit))
)))

I see no i in that line.

Bob

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #20 2012-12-23 01:16:29

anonimnystefy
Real Member

Offline

### Re: Lisp

Bob, you are brilliant!

The picture is given below.

Uploaded Images

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #21 2012-12-23 02:21:08

bob bundy
Moderator

Offline

### Re: Lisp

That's a great fractal.  Well done!

So you could (i) improve the resolution or (ii) add colour or (iii) do a new fractal.

Bob

btw  Is now a good moment to admit I cannot program in Lisp ?  I was bluffing!

You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei

## #22 2012-12-23 02:34:47

anonimnystefy
Real Member

Offline

### Re: Lisp

I am going with (i) first.

The other two will have to wait a bit...

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## #23 2013-05-18 14:21:56

Agnishom
Real Member

Offline

### Re: Lisp

Would you give a link to download your compiler?

Last edited by Agnishom (2013-05-18 14:27:05)

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
'The whole person changes, why can't a habit?' -Alokananda

## #24 2013-11-15 23:24:04

Agnishom
Real Member

Offline

### Re: Lisp

Hi stefy;

What is a good way to learn Lisp?

'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
'The whole person changes, why can't a habit?' -Alokananda

## #25 2013-11-16 05:12:09

anonimnystefy
Real Member

Offline

### Re: Lisp

Well, I never learned it much, so I cannot say for sure, but what I used for learning was the book On Lisp which covers the basics. Just Google "onlisp.pdf".

The limit operator is just an excuse for doing something you know you can't.
“It's the subject that nobody knows anything about that we can all talk about!” ― Richard Feynman
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment

## Board footer

Powered by FluxBB