0
votes

Hi I got the error mcar: contract violationexpected: mpair? given: () while running these code:

(define helpy 
  (lambda (y listz)
    (map (lambda (z) (list y z))
         listz)))

(define print
  (lambda (listy)
    (cond
      ((null? list) (newline))
      (#t (helpy (car listy) (cdr listy))
          (print (cdr listy))))))

My code is trying to return pairs in a list. For example, when I call (print '(a b c)) it should return ((a b) (a c) (b c)).

I just fix and update my code, now it don't return error but I can only get pair ( (a b) (a c), when running these code:

(define helpy

(lambda (y listz)

(map (lambda (z) (list y z))

listz)))

(define print

(lambda (listy)

(cond

((null? listy) (newline))

(#t (helpy (car listy) (cdr listy)))

(print (cdr listy)))))

I think that I got something wrong with the recursion

2

2 Answers

0
votes

There are a couple of problems with the code. First, by convention the "else" clause of a cond should start with an else, not a #t. Second, the null? test in print should receive listy, not list. And third, you're not doing anything with the result returned by helpy in print, you're just advancing print over the cdr of the current list without doing anything with the value returned by the recursive call. Try this instead:

(define print
  (lambda (listy)
    (cond
      ((null? listy) (newline))
      (else
       (displayln (helpy (car listy) (cdr listy)))
       (print (cdr listy))))))

displayln is just an example, do something else with the returned result if necessary.

0
votes

I try to implement like this:

#lang racket

(define data '(a b c d))

(define (one-list head line-list)
  (if (null? line-list)
      null
      (cons
       (cons head (car line-list))
       (one-list head (rest line-list)))))

(letrec ([deal-data
           (lambda (data)
             (if (null? data)
                 '()
                 (append
                  (one-list (car data) (rest data))
                  (deal-data (rest data)))))])

  (deal-data data))

run result:

'((a . b) (a . c) (a . d) (b . c) (b . d) (c . d))