Well, I am trying to add two elements to become a list, and from the video of Prof. Colleen Lewis at minute 4 and 53 secs, and from the official racket site I saw that the best practice is to use "cons".
When I did:
(cons (Listof Number) (Listof (Listof Number)))
It worked perfectly well but it gave me a mirror to what I am suppose to get. Therefore I tried:
(cons (Listof (Listof Number)) (Listof Number) )
This resulted in:
Type Checker: Polymorphic function `cons' could not be applied to arguments:
Argument 1:
Expected: a
Given: (Listof (Listof Real))
Argument 2:
Expected: (Listof a)
Given: (Listof Real)
Result type: (Listof a)
Expected result: (Listof (Listof Real))
in: (cons acc (get-min&max-from-mixed-list (first lol)))
This is weird, since the official site says that:
The cons function actually accepts any two values, not just a list for the second argument.
Here is my actual code:
(: min&max-lists (-> (Listof (Listof Any)) (Listof (Listof Number))))
(: tail-min&max-lists (-> (Listof (Listof Any)) (Listof (Listof Number)) (Listof (Listof Number))))
(: get-min&max-from-mixed-list (-> (Listof Any) (Listof Number)))
(define (min&max-lists lol)
(tail-min&max-lists lol '()))
(define (tail-min&max-lists lol acc)
(if (null? lol)
acc
(tail-min&max-lists (rest lol) (cons acc (get-min&max-from-mixed-list (first lol))))))
(define (get-min&max-from-mixed-list mixedList)
(if (null? (sublist-numbers mixedList))
'()
(min&maxRec (sublist-numbers mixedList) (first (sublist-numbers mixedList)) (first (sublist-numbers mixedList)))))
(test (min&max-lists '((any "Benny" 10 OP 8) (any "Benny" OP (2 3)))) => '((8 10) ()))
Here is the code for all kinds of functions that my code uses. These work fine, so there should be no reason to check them:
#lang pl
(require rackunit)
(require racket/trace)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Question 1
(: min&maxRec (-> (Listof Number) Number Number (Listof Number)))
(: min&max (-> Number Number Number Number Number (Listof Number) ))
(define (min&max number1 number2 number3 number4 number5) ; gets all numbers and sends to the min&max recursive part
(min&maxRec (list number2 number3 number4 number5) number1 number1)) ; made all numbers to become a list and has max and min numbers
(define (min&maxRec myList max min)
;; logic: 1) if finished list give back result 2) else check if head of list max or min and recall the function with a shorter list and the new max or min
(if (null? myList)
;return the min and max from myList when finished looking at all numbers
(list min max)
(if (> (first myList) max); is the head max?
(min&maxRec (rest myList) (first myList) min)
(if (< (first myList) min) ; is the head min?
(min&maxRec (rest myList) max (first myList) )
(min&maxRec (rest myList) max min)))))
(test (min&max 2 3 2 7 1) => '(1 7))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Question 2 a
(: sublist-numbers (-> (Listof Any) (Listof Number)))
(: tail-sublist-numbers (-> (Listof Any) (Listof Number) (Listof Number)))
;;This func calls a diff func since the tail-recursion needs an accumelator.
(define (sublist-numbers myList)
(tail-sublist-numbers myList `()))
;;this uses tail-recursion (all calculations are dont before the recursion and are sent with an accumelator)
(define (tail-sublist-numbers myList acc)
(if (null? myList)
acc ; if finished all vars in list
(if (number? (first myList))
(tail-sublist-numbers (rest myList) (cons (first myList) acc)) ; if the head of list is a number add it to acc, and continue working on the rest of the list
(tail-sublist-numbers (rest myList) acc)))) ; else throw this var and work on rest of list