First Attempt at Lisp

I tried out Lisp for the first time. It’s a functional/declarative programming language which is so much more different from an imperative language like Java that I was totally confused at first. I wrote a few lines of simple code.

I think that this sort of coding would be useful for AIs that have to solve problems given certain values and constraints such as chess AIs or Tic Tac Toe AIs. However, AIs that actually “do” something should be programmed in declarative languages like Java and C++.

Code:

(defun add(A B)
(+ A B))

(defun subtract(A B)
(- A B))

(defun multiply(A B)
(* A B))

(defun factorial(N)
(if (or (= N 1) (= N 0))
1
(* N (factorial(- N 1)))))

(print(factorial 8))
(print(add 8 5))
(print(subtract 8 2))
(print(multiply 8 5))

Output:

40320 
13 
6 
40

4 responses to “First Attempt at Lisp

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s