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

You are not logged in.

- Topics: Active | Unanswered

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

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

```
(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:

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

*Last edited by anonimnystefy (2012-12-19 08:40:07)*

Here lies the reader who will never open this book. He is forever dead.

Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and Punishment

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

Anyone have an idea on what I could do next?

Here lies the reader who will never open this book. He is forever dead.

Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and Punishment

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

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

Here lies the reader who will never open this book. He is forever dead.

Taking a new step, uttering a new word, is what people fear most. ― Fyodor Dostoyevsky, Crime and Punishment

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

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

Yes, it quite a capability for recursion.

Here lies the reader who will never open this book. He is forever dead.

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

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

Here lies the reader who will never open this book. He is forever dead.

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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

Bob

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

I use SBCL for CLisp.

Here lies the reader who will never open this book. He is forever dead.

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

Hi Bob

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

Here lies the reader who will never open this book. He is forever dead.

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

Hi Bob

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

Here lies the reader who will never open this book. He is forever dead.

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

Hi Bob

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

Here lies the reader who will never open this book. He is forever dead.

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

Hi Bob

I edited my code a little bit:

```
(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...

*Last edited by anonimnystefy (2012-12-22 02:09:52)*

Here lies the reader who will never open this book. He is forever dead.

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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.

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

Hi Bob

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

Here lies the reader who will never open this book. He is forever dead.

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

Bob, you are brilliant!

The picture is given below.

Here lies the reader who will never open this book. He is forever dead.

Offline

**bob bundy****Moderator**- Registered: 2010-06-20
- Posts: 6,336

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!

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

I am going with (i) first.

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

Here lies the reader who will never open this book. He is forever dead.

Offline

Would you give a link to download your compiler?

*Last edited by Agnishom (2013-05-17 16: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'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

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'

'Humanity is still kept intact. It remains within.' -Alokananda

Offline

**anonimnystefy****Real Member**- From: The Foundation
- Registered: 2011-05-23
- Posts: 15,507

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".

Here lies the reader who will never open this book. He is forever dead.

Offline