0

Starting to learn LISP and wrote two simple programs, which uses functions as params.

The first:

;gnu clisp  2.49.60
(defun pf (x f123) (cond ((null x) nil)
                      (T (cons ( f123 (car x) ) (pf (cdr x) f123)))))

(defun f2 (x) (* x x)) 

(print (pf '(1 2 3 4) 'f2 ) )

The second:

(defun some1(P1 P2 x)
   (if (not( = (length x) 0))
    (cond 
       (
        (or ( P1 (car x) ) ( P2 (car x)) )
        (cons (car x) (some1 P1 P2 (cdr x) ))
        )
       (t (some1 P1 P2 (cdr x) ))
    )
  )
)

(print (some1 'atom 'null '( 5 1 0 (1 2) 10 a b c) )     )

The both of program aren't working. And I don't know how to fix it :(

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
goart dev
  • 1
  • 1
  • you have to write `(funcall f123 x y z)` when a variable holds a functional value, this is not like in Scheme. See https://stackoverflow.com/questions/4578574/what-is-the-difference-between-lisp-1-and-lisp-2 – coredump Nov 26 '20 at 17:58

1 Answers1

0

(funcall f123 x y z) is works, so results:

;gnu clisp  2.49.60
(defun pf (x f123)
  (cond ((null x) nil)
        (T (cons (funcall f123 (car x))
                 (pf (cdr x) f123)))))

(defun f2 (x) (* x x)) 

(print (pf '(1 2 3 4) 'f2))

And

;gnu clisp  2.49.60

(defun eq0(x)
  (if (= x 0)
      t
      nil))

(defun bg10(x)
  (if (> x 10)
      t
      nil))

(defun some1 (P1 P2 x)
  (if (not (= (length x) 0))
    (cond 
       ((or (funcall P1 (car x)) (funcall P2 (car x)))
        (cons (car x) (some1 P1 P2 (cdr x))))
       (t (some1 P1 P2 (cdr x))))))

(print (some1 'eq0 'bg10 '(5 0 0 11)))

Maybe it will be useful for someone :)

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
goart dev
  • 1
  • 1