Subscribe: Web Instruct - Creating Your Website Easy
Language: English
Tags:
car list  car  cdr list  cdr  cond  defun  function defun  function  lisp  list car  list null  list  null list  null  number
Rate this Feed

Feed Details and Statistics
Preview: Web Instruct - Creating Your Website Easy

# WebInstruct

## Web design/development and programming

Last Build Date: Tue, 07 Oct 2014 03:41:54 +0000

LISP Cube List Function

Wed, 17 Oct 2012 20:57:00 +0000

WITHOUT USING LET

(defun cube-list (lst)

(cond ((null lst) nil)
(t ( cons (* (car lst) (car lst) (car lst)) (cube-list(cdr lst)) ) )

)
)

USING LET

(defun cube-list (lst)
(cond ((null lst) nil)
(t ( let ((a (car lst))) (cons (* a a a) (cube-list(cdr lst))) ) )
)
)

LISP Product function

Wed, 17 Oct 2012 20:43:00 +0000

(defun product (list1)
(cond ( (null list1) 1 )
( t (* (car list1) (product (cdr list1))) )
)
)

LISP reverse function

Wed, 17 Oct 2012 20:27:00 +0000

(defun reverse2(list1)

(cond ((null list1) '())
(t ( append (reverse2 (cdr list1)) (list (car list1)) ) )
)
)

LISP Length function

Wed, 17 Oct 2012 20:07:00 +0000

USING COND

(defun my-length (list1)
(cond ( (null list1) 0 )
(t (+ 1 (my-length (cdr list1)) ))
)
)

USING IF

(defun my-length (list1)
( if (null list1) 0 (+ 1 (my-length (cdr list1))) )
)

LISP Last character in list function

Wed, 17 Oct 2012 19:54:00 +0000

(defun last-char(list1)
(cond ( (null list1) nil )
( (null (cdr list1)) (car list1) )     ; null (cdr list1) returns T when 1 item is left in the list
(t (last-char (cdr list1)))
)
)

LISP Sum of list function

Wed, 17 Oct 2012 19:13:00 +0000

USING IF

(defun sum(list)
(if (null list) 0 (+ (car list) (sum(cdr list))) )
)

USING COND

(defun sum(list)
(cond ( (null list) 0 )
( (+ (car list) (sum(cdr list))) )
)
)

USING COND AND ELSE

(defun sum(list)
(cond ( (null list) 0 )
(else (+ (car list) (sum(cdr list))) )
)
)

LISP append lists function

Wed, 17 Oct 2012 18:49:00 +0000

(defun my-append(list1 list2)
( if (null list1) list2 (cons (car list1) (my-append (cdr list1) list2)) )
)

Test Cases

> my-append '() '(a) = (a)
> my-append '(a b c) '(d e f) = (a b c d e f)

LISP Factorial Function

Wed, 17 Oct 2012 18:38:00 +0000

The Math

f(0) = 1 for n = 0
f(n) = (n * factorial(n-1)) for n>0

The Function

(defun factorial (n)
(if (= n 0) 1 ( * n (factorial (- n 1)) ))

)

The Output

> factorial 3 = 6

LISP Product of lists function

Wed, 17 Oct 2012 18:32:00 +0000

(defun my-product (list1 list2)
(if (or (null list1) (null list2)) nil
(let ( (a (* (car list1) (car list2))) ) (cons a (my-product (cdr list1) (cdr list2))))
)
)

Or, without the use of LET

(defun my-product (list1 list2)
(if (or (null list1) (null list2)) nil
(cons (* (car list1) (car list2)) (my-product (cdr list1) (cdr list2)))
)
)

LISP Return Third Character Function

Wed, 17 Oct 2012 18:10:00 +0000

(defun third-char(list)
(car (cdr (cdr list)))
)

> third-char '(a b c d) = C

LISP Palindrome Function

Wed, 17 Oct 2012 18:07:00 +0000

(defun ispalindrome (list)
(equal list (reverse list))
)

> (ispalindrome '(a b b a)) = T

LISP Absolute Difference function

Wed, 17 Oct 2012 18:05:00 +0000

(defun absdiff (x y)
(if (< x y) (- y x) (- x y))
)

> absdiff 3 5 = 2

LISP Triangle Numbers

Tue, 16 Oct 2012 19:38:00 +0000

To calculate the Triangular Numbers in LISP.

N = 1+2+3+...+N
T(n) = 1     if n=1
T(n) = n + T(n-1)     if n>1
T(n) = 0    if n<=0

USING IF

(defun triangle(N)
(if (= N 1) 1 (+ N (triangle(- N 1))))
)

IF is not good for more than one condition

USING COND

(defun tri-cond(number)
(cond ((<= number 0) 0)
((= number 1) 1)
((> number 1) (+ number (tri-cond(- number 1))))
)
)

USING COND WITHOUT A LAST CONDITION

(defun tri-cond(number)
(cond ((<= number 0) 0)
((= number 1) 1)
((+ number (tri-cond(- number 1))))
)
)

LISP Fibonacci recursive function

Tue, 16 Oct 2012 19:24:00 +0000

USING COND

(defun fibo(n)
(cond ((= n 0) 1)
((= n 1) 1)
((> n 1) (+ (fibo(- n 1)) (fibo(- n 2))))
)
)

USING IF

(defun fibo2(n)
(if (or (zerop N) (= N 1)) 1
(+ (fibo2(- n 1)) (fibo2(- n 2))))
)

LISP Recursive Factorial function

Tue, 16 Oct 2012 19:22:00 +0000

(defun factorial(n)
(if (= n 1) 1 (* n (factorial(- n 1))))
)

Recursive LISP function to find the power of a number

Tue, 16 Oct 2012 19:21:00 +0000

(defun power(base exp)
( if (eq exp 0) 1 ( * base (power base (- exp 1)) ) )
)

power 3 2 = 9

Thu, 03 Feb 2011 18:20:00 +0000

Here is how to remove the box-shadow and border-radius CSS properties from a whole website or an element. This can be used to remove these properties from the Wordpress 3 TwentyTen theme when you are creating a child theme.

*
{