Math Is Fun Forum

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

You are not logged in.

#1 2012-12-19 07:15:16

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Lisp

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
The knowledge of some things as a function of age is a delta function.

Offline

#2 2012-12-19 21:37:36

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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
The knowledge of some things as a function of age is a delta function.

Offline

#3 2012-12-19 22:07:25

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

Re: Lisp

Can you do graphics with it?

If so, it is the ideal language for fractals.

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#4 2012-12-19 22:12:41

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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
The knowledge of some things as a function of age is a delta function.

Offline

#5 2012-12-19 22:37:35

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

Re: Lisp

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

If it works, post up the pictures.

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#6 2012-12-19 22:42:43

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

#7 2012-12-19 23:02:52

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

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


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#8 2012-12-19 23:04:24

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

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

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

Re: Lisp

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

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#10 2012-12-20 07:20:17

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

I use SBCL for CLisp.


“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
The knowledge of some things as a function of age is a delta function.

Offline

#11 2012-12-20 12:33:33

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

#12 2012-12-20 15:52:57

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

Re: Lisp


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

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

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

#14 2012-12-21 11:23:52

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

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


“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
The knowledge of some things as a function of age is a delta function.

Offline

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

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

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


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

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

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

#17 2012-12-22 01:31:27

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

Re: Lisp

Ok.  A common mistake which you can easily fix:

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

smile

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.


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#18 2012-12-22 02:05:55

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

#19 2012-12-22 02:07:55

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

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


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#20 2012-12-22 02:16:29

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

Bob, you are brilliant!

The picture is given below.


“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
The knowledge of some things as a function of age is a delta function.

Offline

#21 2012-12-22 03:21:08

Bob
Administrator
Registered: 2010-06-20
Posts: 10,621

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.

smile

Bob

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


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#22 2012-12-22 03:34:47

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

Re: Lisp

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.
“Taking a new step, uttering a new word, is what people fear most.” ― Fyodor Dostoyevsky, Crime and Punishment
The knowledge of some things as a function of age is a delta function.

Offline

#23 2013-05-17 16:21:56

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,996
Website

Re: Lisp

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'
I'm not crazy, my mother had me tested.

Offline

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

Agnishom
Real Member
From: Riemann Sphere
Registered: 2011-01-29
Posts: 24,996
Website

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'
I'm not crazy, my mother had me tested.

Offline

#25 2013-11-15 06:12:09

anonimnystefy
Real Member
From: Harlan's World
Registered: 2011-05-23
Posts: 16,049

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


“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
The knowledge of some things as a function of age is a delta function.

Offline

Board footer

Powered by FluxBB