0

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

0

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

6 Comments

"The last clause of a cond should start with an else, not a #t". Really? I'm don't write much scheme, but I thought that was a stylistic thing that doesn't have universal agreement.
@amalloy #t works, but it's not the usual way to write it - that's more of a Common Lisp convention than Scheme's
@ÓscarLópez: Thanks you The racket can't define the term displayln in your code, anw I update my code, it now doesn't return error, but I only can get pair (a b)(a c) insted of (a b)(a c)(b c), I think it has something wrong with recursion???
@Daniel try with display then. The code above was tested in Racket using the language @lang racket and it prints the results
@ÓscarLópez: Do you have any ideas, that I can expand the function print, for example I run print with (print '((a b c)(b d e) it can return ((a b)(a c)(b c)(b d)(b e)(d e)
|
0

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))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.