Schema Function Definition

So, I was stuck trying to learn this language of the Scheme that I heard about from a friend of mine. What he said, what I have to do is to start small and work to understand him. Therefore, after reading a tutorial, which he briefly talked about programming Schemes, I am a little puzzled by how this works.

To a large extent, I am trying to ask that if I wanted to add a definition to the function being called, say β€œevens” from the pair of lists that I defined:

(DEFINE list0 (LIST 'j 'k 'l 'm 'n 'o 'j) ) (DEFINE list1 (LIST 'a 'b 'c 'd 'e 'f 'g) ) (DEFINE list2 (LIST 't 'u 'v 'w 'x 'y 'z) ) (DEFINE list3 (LIST 'j 'k 'l 'm 'l 'k 'j) ) (DEFINE list4 (LIST 'n 'o 'p 'q 'q 'p 'o 'n) ) (DEFINE list5 '((ab) c (ded) c (ab) ) (DEFINE list6 '((hi) (jk) l (mn)) ) (DEFINE list7 (f (ab) c (ded) (ba) f) ) 

so that it performs the following task: evens

which I have already created an adder function, which I suppose should be:

 (DEFINE (adder lis) (COND ((NULL? lis) 0) (ELSE (+ (CAR lis) (adder (CDR lis)))) )) 

, which could even be for determining if I wanted this to be performed as a task below:

Evens: (evens 1st) should return a new list formed of even elements made by 1st.

 (evens '(abcdefg)) which would/should return: (bdf) 

and

 (evens (LIST 't 'u 'v 'w 'x 'y 'z)) which would/should return: (tvxz) 

and

 (evens '(f (ab) c (ded) (ba) f) ) which would return: ((ab) (ded) f) 

and both (evens '()) and (evens' (a))

will return an empty list.

I walk the trail by mistake to practice, but I am completely lost. thanks in advance

Ok, I think I came up with a recursive function for my example, which I was trying to solve:

  (define mylist '(1 2 3 4 5 6 7)) (define (evens lst) (define (do-evens lst odd) (if (null? lst) lst (if odd (do-evens (cdr lst) #f) (cons (car lst) (do-evens (cdr lst) #t))))) (do-evens lst #t)) 

any thoughts?

+4
source share
3 answers

OK, therefore, to perform a recursive function that iterates through the list, selecting some items to return and not others, the main template will be

 (define (recur l) (cond ((null l) '()) ((??????) (cons (car l) (recur (cdr l)))) (else (recur (cdr l))))) 

What does it mean,

  • Is the list null? Returns null.
  • If the list is not null, should we keep the first item in the list? Ok, let it save it and add it to everything (recur (cdr l)) returns
  • Otherwise, just return all recur (cdr l) .

I hope this clearly suggests that a simple recursive list function only looks at the first element of the list, determines what to do with it, and then calls itself again with the rest of the list. Recursion is, in a sense, a way to simplify calculations, associated only with the first part of the data and leaving another function to worry about the rest of the data (the other function is actually the same function that you called for the first time, but try don't worry about it). For example, a recursive function that returned only those elements that were even numbers looked like this:

 (define (even-numbers l) (cond ((null? l) '()) ((even? (car l)) (cons (car l) (even-numbers (cdr l)))) (else (even-numbers (cdr l))))) 

However, your case is a little more complicated; you need to discard the first element, and then in each subsequent function call do the opposite of the last call.

One way to do this is to switch to a logical state, throwing it over every time. Note that since the evens function does not take values, you need to define another internal function and repeat it:

 (define (evens l) (let loop ((lst l) (keep #f)) (cond ((null? lst) '()) ((eq? keep #t) (???????) (else (loop (cdr l) #t))))) 

You should be able to invent what goes into (???????) for yourself, given the previous examples. I think I have done too much work for you.

Another way to do this is with the mutually recursive functions evens and odds. Consider that

  • evens is a function that discards the first element and then works through the list, each time doing the opposite (saving or discarding) what it did the last time.
  • odds is a function that stores the first element and then works through the list, each time doing the opposite (saving or discarding) what it did the last time.

That means you can define

  • evens as a function that ignores the first element and simply calls odds in the rest of the list.
  • odds as a function that holds the first element, calls evens in the rest of the list and minus on the first element at the beginning of the result.

It would look like

 (define (evens l) (cond ((null? l) '()) (else (odds (??????))))) (define (odds l) (cond ((null? l) '()) (else (cons (car l) (evens (??????)))))) 

Hope this makes sense.

If you really want to understand recursion on a schema, buy a copy of "The Little Schemer" and get started with it.

+2
source

First of all, the adder function has nothing to do with your question, right?

Think of the evens function. You already said that (evens '()) and (evens '(a)) should return an empty list. What should return (evens '(ab)) ? What about (evens '(ab ...)) ? Do you see how to set up recursion?

+2
source

How about this:

 (define (evens l) (cond ((null? l) '()) ((null? (cdr l)) '()) (else (cons (cadr l) (evens (cddr l)))))) 

But I like the answers and the odds!

0
source

Source: https://habr.com/ru/post/1445152/


All Articles