Okay, so:

    (defn evaltree [tree]
      (cons (eval tree) (rest tree)))

That evaluates the tree, assuming that (rest tree) only contains numbers.

    (eval-tree '(+ 3 9))

However, we also want to evaluate the arguments in the same way. The
arguments are a list, and when we want to do something to every item of a
list, we use a map or for loop:

    (defn evaltree [tree]
      (cons (eval tree) (map (fn [arg] ...) (rest tree))))

This is going to get a little long for one line, so let's spread it out on
multiple lines

    (defn evaltree [tree]
      (cons
       (eval tree)
       (map
        (fn [arg] ...)
        (rest tree))))

The question is, what should be in that mapped function? Well, if the
argument is not a list, then we don't need to do anything:

    (defn evaltree [tree]
      (cons
       (eval tree)
       (map
        (fn [arg]
          (if (list? arg)
            ...
            arg))
        (rest tree))))

And if the argument is a list... well, we already have a function do deal
with that: evaltree!

    (defn evaltree [tree]
      (cons
       (eval tree)
       (map
        (fn [arg]
          (if (list? arg)
            (evaltree arg)
            arg))
        (rest tree))))

The disadvantage of this particular implementation is that, while simple,
it evaluates the inner forms more than once. This means it's probably not
as efficient as it could be, and side-effectful functions would trigger
multiple times.

- James


On 14 November 2015 at 19:40, <stevegarrid1...@gmail.com> wrote:

> Ok by starting with the basic (Def a '(+ 3 9))
>
>
> I have
> (Defn evaltree [tree]
> (Cons (eval tree) tree))
>
> But I can't seem to get it to work or drop the plus from the list. I'm
> really struggling with this
>
> Thanks for your help
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to