Needs["CSM`Lisp`"]

(* Basic operations on Lisp lists *)

(* mapping a function over a list *)

map[f_, l_] :=
	If[ nullQ[l],
		nil,
		cons[ f[car[l]], map[f, cdr[l]] ]
	]

(* appending *)

append[l_, e_] :=
	If[ nullQ[l],
		list[e],
		cons[ car[l], append[cdr[l], e] ]
	]

(* reversing elements *)

reverse[l_] := 
	If[ nullQ[l],
		nil,
		append[ reverse[cdr[l]], car[l] ]
	]

(* removing the fist occurrence of an element *)

remove[l_, e_] := 
        Which[
                nullQ[l],   nil,
                car[l]===e, cdr[l],
                True,       cons[ car[l], remove[cdr[l], e] ]
        ]

(* insertion into ordered list *)

insert[l_, e_] :=
        Which[
                nullQ[l],   list[e],
                e < car[l], cons[e, l],
                True,       cons[ car[l], insert[cdr[l], e] ]
        ]

(* replacing all occurrences of a by b *)

substAll[l_, a_, b_] :=
        Which[
          atomQ[l], If[l === a, b, l],
          nullQ[l], nil,
          True,     cons[ substAll[car[l], a, b],
                          substAll[cdr[l], a, b] ]
        ]

(* membership test *)

memberQ[l_, e_] := 
        Which[
          nullQ[l],   False,
          e===car[l], True,
          True,       memberQ[cdr[l], e]
        ]

(* version of nested lists *)

memberAllQ[l_, e_] :=
        Which[
          atomQ[l], l === e,
          nullQ[l], False,
          True,     memberAllQ[car[l], e] || memberAllQ[cdr[l], e]
        ]
