On 1/2/2018 12:43 PM, Sanjeeb Sinkhada wrote:
hi could anyone help me with this question?
Write a function that will take a list of pairs and return just the
pair comprising the first element of the first pair and the second
element of the last pair.
> (reduce-pairs ’())
> (reduce-pairs ’((1 2) (3 4)))
> (reduce-pairs ’((1 2) (3 4) (5 6)))
I have got this so far
(define reduce-pair(λ(x y)(cond
(cons( car x)
(cons ( list-tail (second y)))))))
but its completely wrong
thanks in advance
Yup, it definitely is wrong. <grin> This smells like homework and the
homework policy in this forum is to help but not answer directly.
First, your examples have a single list argument, but your function is
defined to have 2 arguments. Why?
Second, what should happen if the list contains only 1 pair or contains
elements that are not pairs?
Third, are you supposed to solve the problem by walking the list, or can
you use any pre-defined list functions?
Think about how you would get the first and last elements of the list.
Then think about how you get the first and second values of those
pairs. Then think about how to make a new pair from those values. If
you can use any predefined function, then you're at least half way there
already ... you just need to put it together.
You received this message because you are subscribed to the Google Groups "Racket
To unsubscribe from this group and stop receiving emails from it, send an email
For more options, visit https://groups.google.com/d/optout.