Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 780587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:05:50+00:00 2026-05-14T20:05:50+00:00

The problem this time is to get the median of three values (easy) I

  • 0

The problem this time is to get the median of three values (easy)

I did this:

(define (med x y z) (car(cdr(x y z)))

and it was accepted but when testing it:

(med 3 4 5)

I get this error:

Error: attempt to call a non-procedure
(2 3 4)

And when entering letters instead of number i get:

(md x y z)

Error: undefined varia
y
(package user)

Using something besides x y z I get:

(md d l m)

Error: undefined variable
d
(package user)

the question was deleted dont know how anyway

write a function that return the median of 3 values

Sorry for editing the question I got that I should put the values in order first not just a sill car and cdr thing so I did so

33> (define (med x y z)
   (if(and(

      (<x y) (<y z) y

               if(and(

                    (<y x) (<x z) x z)))))

Warning: invalid expression
         (if (and< (<x y) (<y z) y if (and ((<y x) (<x z) x z))))

but as u see Im getting a warning so what is wronge ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:05:51+00:00Added an answer on May 14, 2026 at 8:05 pm

    Note that as defined, (med . rest) is equivalent to (cadr rest) (except that med only takes three values). Personally, I would expect a function that’s supposed to return the median of values to return, well, the median, regardless of list order. For example, (med 4 2 5) would return 4 and (3 0 9 6 5) would return 5.

    As for the syntax error (which doesn’t matter so much for writing med, since there is a better way using sort, length and list-ref), you don’t have your parentheses in the right spots. Here’s another way of writing what you have now, lining up terms with their siblings and to the right of their ancestors:

    (if (and (
               (<x y) 
               (<y z) 
               y
               if
               (and (
                      (<y x) 
                      (<x z) 
                      x 
                      z
    )   )    ) )    )
    

    The format for if is:

    (if test-expr true-expr false-expr)
    

    All of your terms are sub-terms of the conditional, and there’s no true-expr or false-expr. You’d want to write your code as:

    (if (and ...)
        y
        (if (...) ; we know that (not (and (< x y) (< y z))
            x
            z))
    

    Note that you might be ably to simplfy the later tests, since you know the earlier tests are false.

    You could also use a cond, which is clearer than a nested sequence of ifs:

    (cond (test result)
          (test result)
          (test result)
          ... )
    

    For your code, it would be:

    (cond ((and ...) y)
          ((...) x)
          (else z))
    

    There’s nothing too special about else (it’s a syntactical construct rather than an expression, not that it matters much). You can use any value that evaluates to true (e.g. #t) instead.

    Remember, parentheses surround both the function and arguments in Lisp ((foo 1 2 3), not foo(1, 2 ,3)), unlike mathematical notation where parentheses surround just the arguments.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been tackling this problem for some time. I get the image from
This is my problem. Every time I load the page, I get an alert
I had this problem some time ago and I gave up but lately it
I'm having a really difficult time debugging this problem. Whenever I try to establish
another sql problem of mine .. this time the reader doesn't work properly (
I got another problem, this time with tables and their borders in HTML (4.01,
Hello again everyone, This time I have a problem with Intent & Extras. here's
OK I'm stuck again, this time it's a problem with the regex... Was searching
This problem has been bugging me for a long time. For example, the code
I add this problem for quite some time when using NetBeans. Recently I add

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.