From: sharkman on
This problem involves arithmetic expressions, which may or may not be
numbered. You may need to use the following help functions in addition
to the buit-in functions including eval which evaluates an arithmetic
expression. For example, (eval ‘(+ 1 2)) returns 3.
Code:

(define (atom? x) (not (list? x)))
(define build-aexp
(lambda (op sub1 sub2)
(cons op (cons sub1 (cons sub2 '())))))

(define numbered? (lambda (aexp)
(cond
((atom? aexp) (number? aexp))
(else (and (numbered? (cadr aexp))
(numbered? (caddr aexp)))))))

a)Write a function simplify which evaluates as much of an arithmetic
expression as possible. For example,
(simplify '(* 2 (+ (* a (+ 3 5)) (+ 1 4)))) should return (* 2 (+ (* a
5))
b)Write a function remove-noops which eliminates any additions of 0 or
multiplications by 1. For example, both
(remove-noops '(* 3 (* 1 (+ a 0)))) and (remove-noops (simplify '(* 3
(* 1 (+ a (- 1 1)))))) should return (* 3 a).
From: Pillsy on
On May 4, 6:11 pm, sharkman <a.las...(a)aui.ma> wrote:

> This problem involves arithmetic expressions, which may or may not be
> numbered. You may need to use the following help functions in addition
> to the buit-in functions including eval which evaluates an arithmetic
> expression.

For what purpose are you posting this?

If (as seems likely) you would like assistance with a homework
problem, you would probably get more useful replies if you showed what
you've tried already, and perhaps provide some test cases that aren't
obviously bogus.

Cheers,
Pillsy
From: sharkman on
On May 4, 10:26 pm, Pillsy <pillsb...(a)gmail.com> wrote:
> On May 4, 6:11 pm, sharkman <a.las...(a)aui.ma> wrote:
>
> > This problem involves arithmetic expressions, which may or may not be
> > numbered. You may need to use the following help functions in addition
> > to the buit-in functions including eval which evaluates an arithmetic
> > expression.
>
> For what purpose are you posting this?
>
> If (as seems likely) you would like assistance with a homework
> problem, you would probably get more useful replies if you showed what
> you've tried already, and perhaps provide some test cases that aren't
> obviously bogus.
>
> Cheers,
> Pillsy

this is what I figured so far:

(define (syf ls)
(cond
((null? ls) '())
((atom? ls) ls)
((not(number? (car ls)))((number? (cadr ls)))
(else (append (syf (cadr ls))
(list (syf (car ls)))))))
From: Barry Margolin on
This is comp.lang.lisp. Comp.lang.scheme is over that way. -->

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Pascal J. Bourguignon on
sharkman <a.lasfar(a)aui.ma> writes:

> This problem involves arithmetic expressions, which may or may not be
> numbered. You may need to use the following help functions in addition
> to the buit-in functions including eval which evaluates an arithmetic
> expression. For example, (eval ‘(+ 1 2)) returns 3.
> Code:


> (define (atom? x) (not (list? x)))
> (define build-aexp
> (lambda (op sub1 sub2)
> (cons op (cons sub1 (cons sub2 '())))))
>
> (define numbered? (lambda (aexp)
> (cond
> ((atom? aexp) (number? aexp))
> (else (and (numbered? (cadr aexp))
> (numbered? (caddr aexp)))))))

The indentation is wrong. Use emacs to have your code indented
correctly automatically, and to send it to usenet!


> (define (atom? x) (not (list? x)))
>
> (define build-aexp
> (lambda (op sub1 sub2)
> (cons op (cons sub1 (cons sub2 '())))))

This can be written simply as:

(define build-aexp list)


> (define numbered? (lambda (aexp)
> (cond
> ((atom? aexp) (number? aexp))
> (else (and (numbered? (cadr aexp))
> (numbered? (caddr aexp)))))))

This lacks abstraction. cadr and caddr don't mean anything for an aexp.
You should write:

(define operator car)
(define left-argument cadr)
(define right-argument caddr)
; (These functions could be generated automatically by a define-structure
; macro, but this is beyond the scope of this answer).



(define numbered?
(lambda (aexp)
(cond
((atom? aexp) (number? aexp))
(else (and (numbered? (left-argument aexp))
(numbered? (right-argument aexp)))))))


> a)Write a function simplify which evaluates as much of an arithmetic
> expression as possible. For example,
> (simplify '(* 2 (+ (* a (+ 3 5)) (+ 1 4)))) should return (* 2 (+ (* a
> 5))

Yes.

> b)Write a function remove-noops which eliminates any additions of 0 or
> multiplications by 1. For example, both
> (remove-noops '(* 3 (* 1 (+ a 0)))) and (remove-noops (simplify '(* 3
> (* 1 (+ a (- 1 1)))))) should return (* 3 a).

Ok.


So what's the difficulty?

--
__Pascal Bourguignon__