In writing a function evalidate, I need it to be able to take a list
of paramters in Cambridge Normal Form, of any size or length, such as:
(evalidate (func1 (func1 7 12) (func2 13 8)))
that will then return what would be easily done in another language.
The only two functions that are possible to be used in the "evalidate"
are the func1 and func2. I have the func1 and func2 working great,
but its this evalidate function giving me logical problems -- I just
can't visualize how it should be working. I assume there is a need
for a conditional and then recursive calls, but I have never used
functional paramters. And, this is my first experience with Lisp
also.
Thanks for any help!
······@yahoo.com (Vigile) writes:
> In writing a function evalidate, I need it to be able to take a list
> of paramters in Cambridge Normal Form, of any size or length, such as:
>
> (evalidate (func1 (func1 7 12) (func2 13 8)))
>
> that will then return what would be easily done in another language.
> The only two functions that are possible to be used in the "evalidate"
> are the func1 and func2. I have the func1 and func2 working great,
> but its this evalidate function giving me logical problems -- I just
> can't visualize how it should be working.
You said it would be easiliy done in another language. So show us what
you'd want to write in some other language. That would allow us to help
you with the syntax issues. We can't do your whole homework for you.
(In fact, though, I found your description of the problem
incomprehensible, so couldn't have helped you in more detail
even if I wanted. Sorry.)
> I assume there is a need
> for a conditional and then recursive calls, but I have never used
> functional paramters.
Do you mean parameters to functions? Certainly if you've written any
function you must have done this.
(defun f (x y) (+ x y))
If you mean passing function names as parameters, I don't see any place
where that needs to happen. It can be done. e.g.,
(defun f (f x y)
(list (funcall f x) (funcall f y)))
(f '- 3 -4) => (-3 4)
(f 'list 'a 'b) => ((A) (B))
> And, this is my first experience with Lisp also.
Welcome.
Don't let the obscure nature of this problem lead you to believe that
Lisp is obscure. Lisp is a very straightforwad language that happens to
allow you to do some interesting things most other languages don't
do. Seems like no one ever uses it to demonstrate the ordinary things,
and so people come away thinking nothing ordinary is easy to do in Lisp.
······@yahoo.com (Vigile) writes:
> In writing a function evalidate, I need it to be able to take a list
> of paramters in Cambridge Normal Form, of any size or length, such as:
>
> (evalidate (func1 (func1 7 12) (func2 13 8)))
>
> that will then return what would be easily done in another language.
> The only two functions that are possible to be used in the "evalidate"
> are the func1 and func2. I have the func1 and func2 working great,
> but its this evalidate function giving me logical problems -- I just
> can't visualize how it should be working. I assume there is a need
> for a conditional and then recursive calls, but I have never used
> functional paramters. And, this is my first experience with Lisp
> also.
There's not difficulty with parameters that are function. In
Common-Lisp, you have two functions to invoke the function denoted by
the parameter: funcall and apply.
When you have a fixed number of argument and you have them, you can
use funcall:
(setq fun (function +)
x 1
y 2)
(funcall fun x y) --> 3
If you have a variable or unknown number of arguments, you must use
the apply function, which takes as its last arguments a list of
additional arguments:
(setq fun (function *)
args (list 1 2 3))
(apply fun args) --> 6
--
__Pascal_Bourguignon__ http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.
······@yahoo.com (Vigile) wrote in message news:<····························@posting.google.com>...
> In writing a function evalidate, I need it to be able to take a list
> of paramters in Cambridge Normal Form, of any size or length, such as:
>
> (evalidate (func1 (func1 7 12) (func2 13 8)))
Maybe you want EVALIDATE to be a macro instead? So that rather than
receiving the result of evaluating the function call (FUNC1 (FUNC1
...) ...), it receives the list which represents the source code of
that expression?
Or maybe you want to keep EVALIDATE as a function, but then you need
to ensure that the evaluation of the argument is suppressed, by using
quote:
;; my custom ``evalidation''
(evalidate '(func1 ...)))
;; ... following the interface design of the standard Lisp
evaluator:
(eval '(func1 ...)))
> that will then return what would be easily done in another language.
> The only two functions that are possible to be used in the "evalidate"
> are the func1 and func2. I have the func1 and func2 working great,
So they are just ordinary Lisp functions? What is left for EVALIDATE
to do then? Are you sure you are not trying to reimplement the
standard EVAL function from scratch?
Or is your task to implement a code-walker which validates that the
expression uses only the functions FUNC1 and FUNC2, and then evaluates
the expression? (evalidate == validate + eval?)
> but its this evalidate function giving me logical problems -- I just
> can't visualize how it should be working. I assume there is a need
> for a conditional and then recursive calls, but I have never used
> functional paramters.
You probably need to pass in the expression as an unevaluated list,
and then do some recursion to walk that list and ensure that the first
element of every list or sublist is FUNC1 or FUNC2.
(defun check-expression-uses-only (expression function-list)
"Return T if EXPRESSION only uses the functions or operators
in FUNCTION-LIST."
(cond
((atom expression) t) ;; atoms are okay
((null expression) t) ;; NIL likewise
((not (member (first expression) function-list)) nil)
(t (every #'(lambda (sub-expr)
(check-expression-uses-only sub-expr
function-list))
(rest expression)))))
;; check that (x (y 1 2 3) 4) uses only x and y functions.
(check-expression-uses-only '(x (y 1 2 3) 4) '(x y)) ;; -> T
;; oops, z introduced, so the check fails
(check-expression-uses-only '(x (z 1 2 3) 4) '(x y)) ;; -> NIL