1
votes

Here is my Racket problem:

Define a function that takes a list as an argument. It should return a boolean (i.e. #t or #f) indicating whether the list is sorted in ascending order. You may not use the built-in sorted? function. Your implementation must be recursive.

Input: A list of elements of homogenous data type, either numbers or strings.

Output: A boolean value that indicates whether the elements of the list are sorted in strictly increasing order. If the list contains heterogenous data types, then throw an error (using the error function) with the message “ERROR: List contains heterogenous data types”.

So when I type this function have to gave me this ERROR

(my-sorted? '(7 "spam" 9)) ! ERROR: List contains heterogenous data types

BUT for mine its gave me this

(my-sorted? '(7 "spam" 9)) : contract violation expected: real? given: "spam" argument position: 2nd other arguments...:

Here what I have

(define (my-sorted-int? lst)
  (define size (length lst))
  (if (< size 2)
      #t
      (if (null? lst)
          #t
          (if (> (car lst) (car (rest lst)))  <======= Gave me Error
              #f
              (my-sorted-int? (rest lst) )))))

(define (my-sorted-string? lst)
  (define size (length lst))
  (if (< size 2)
      #t
      (if (null? lst)
          #t
          (if (string>? (car lst) (car (rest lst)))
              #f
              (my-sorted-string? (rest lst) )))))

(define (my-sorted? lst)
  (if (string? (car lst))
      (my-sorted-string? lst)
      (my-sorted-int? lst)))
1
Show what you have so far. - soegaard
I posted what I have and thanks a lot - user8286060
You could insert a in my-sorted-int? that (car (rest last)) is an integer - and display an error if not. - soegaard
you can check my code now , I show when the error when I type my code - user8286060
Since > expects two integers and (car (rest last)) could be a string you need to insert a check before. - soegaard

1 Answers

1
votes

So you know the left side is a number since it was the value you initially checked, but the right side also needs to be a number to be able to do > on it. Thus you need something like:

(if (number? (cadr lst))
    (if (> (car lst) (cadr lst)) ...)
    (error "ERROR: List contains heterogenous data types"))

You might want to use cond to get a flatter structure. Also you have made two identical procedures to deal with integers and strings when you could just made one and pass the things that are different to make your code more DRY:

(define (my-sorted? lst)
  (define (my-helper? correct-type? greater-than?)
    (let loop ((e (car lst))
               (lst (cdr lst)))
      (cond ((null? lst) #t)
            ((not (correct-type? (car lst)))
             (error "ERROR: List contains heterogenous data types"))
            ((greater-than? e (car lst)) #f)
            (else (loop (car lst) (cdr lst))))))

  (cond ((null? lst) #t)
        ((string? (car lst)) (my-helper? string? string>?))
        (else (my-helper? number? >))))