Problem with homework scheme, need help

Well, one of my problems with homework is to take the list of lists and return the car of each subscription to the list. I have where I can print the values, but this is not a list. Honestly, I do not know how to list. Here is what I got:

(define (car-print alist)
  (if (null? alist)
      (newline)
      (begin
         (write (car (car alist))) (display " ")
  (car-print(cdr alist)))))

This is a cheap way to do this, maybe some help in solving this problem would be much appreciated. Not necessarily a complete answer, but steps to get there. Thank.

+3
source share
3 answers

My circuit is a little rusty. And I only have a dr schema translator on hand ...

You need to use your list and return only the first element of each sublist.

, cons, , ( ).

, .

, - , ( ) ( )

(cons 1) => error: cons need two parameters
(cons 1 null) => (list 1)
(cons 1 2) => error: need a value (first parameter) and a list (the second one)
(cons 1 (cons 2 null) => (list 1 2)

. , , ,

(define (car-list alist)
  (cond
    ((null? alist) null)
    (else (cons (car(car alist)) (car-list (cdr alist))))
  )
)
; tail recursion version usage: (car-acc-list alist null)
(define (car-acc-list alist acc)
  (cond
    ((null? alist) acc)
    (else (car-acc-list (cdr alist) (cons (car(car alist)) acc)))
  )
)

cond, if, , . : ( ) , cdr. ((null? Alist) null), . , .

, / .

, drscheme , (lgpl). , , .

+1

map:

> (define lst '((1 2 3) (4 5 6) (7 8 9)))
;Value: lst
> (map car lst)
;Value: (1 4 7)
+1

, , . :

cons : n l -> (n l)
Construct a list out of an element and a list, placing the element n
at the head of the list

car : l -> n
Return the head of the list

cdr : l -> l
Return the tail of the list

You want to write a firstl function that uses a list of lists and returns a list. Here are some examples:

(firstl (list (list 1 2 3) (list 9 2 3) (list 4 7 3))) -> (1 9 4)
(firstl (list (list 1 2 3))) -> (1)
(firstl '()) -> ()

The final example should give you your first key: if the argument is an empty list, return an empty list.

(define (firstl lol)     ; list-of-lists (no laughing!)
  (if (null? lol)
    '()
    ....    ; more stuff goes here for the recursive "else" clause. Hint: use cons
))
0
source

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


All Articles