Sort a list in Racket based on the operator - list

How can I sort and merge two lists based on the operator in Racket-Plait? > ascending, < descending order. This is what I have so far, but I do not have any idea what to do next.
(define (merge [op : (Number Number -> Boolean)]
[int-list1 : (Listof Number)]
[int-list2 : (Listof Number)]) : (Listof Number)
(cond
[(equal? op <) "something"]
[(equal? op >) "do something"]))
(test (merge < '(1 4 6) '(2 5 8))
'(1 2 4 5 6 8))

I think you're assuming that this is more complicated than it is.
You're not supposed to do different things depending on the value of op - op is the ordering predicate you should use for merging and the predicate that the input lists are ordered by.
(In contrast to many other languages, symbols like < or > are not "special" in any way, they are identifiers just like less, or greater, or op)
Here is a skeleton special case to get you started; the interesting bits are left as an exercise:
(define (merge [l1 : (Listof Number)]
[l2 : (Listof Number)]) : (Listof Number)
(cond [(empty? l1) l2] ;; If either list is empty,
[(empty? l2) l1] ;; the result is the other list.
[(< (first l1) (first l2)) ... fill this in ...]
[else ... fill this in ...]))
then, notice that this is equivalent to the code above:
(define op <)
(define (merge [l1 : (Listof Number)]
[l2 : (Listof Number)]) : (Listof Number)
(cond [(empty? l1) l2] ;; If either list is empty,
[(empty? l2) l1] ;; the result is the other list.
[(op (first l1) (first l2)) ... fill this in ...]
[else ... fill this in ...]))
and then you pass op as a parameter instead, and you're done.

Related

DrRacket Using accumulator as List-ref index

I am trying to write a function that take a list and iterates over each element in the list if the number is even I want that number to be added to the previous number in the list.
I was thinking an accumulator will count up from 0 with each iteration giving a position for each element in the list.
If the number in the list is even I want to add that number to the previous number in the list.
Hence why I am trying to use the accumulator as an index for list-ref. I don't know how to write it to get the accumulator value for the previous iteration (+ i (list-ref a-list(- acc 1)))?
(define loopl (lambda (l)
(for/fold
([acc 0])([i l])
(cond
[(even? i)(+ i (list-ref (- acc 1) l))]
enter image description here
The question is not quite clear about the value to be returned by this function:
this answer assumes that it is a total of even elements together with their previous elements.
The function is developed using the
HtDF (How to Design Functions)
design method with a BSL (Beginning Student language) in DrRacket.
Start with a stub, incorporating signature and purpose, and a minimal "check-expect" example:
(Note: layout differs slightly from HtDF conventions)
(define (sum-evens-with-prev xs) ;; (Listof Integer) -> Integer ; *stub define* ;; *signature*
;; produce total of each even x with its previous element ; *purpose statement*
0) ; *stub body* (a valid result)
(check-expect (sum-evens-with-prev '()) 0) ; *minimal example*
This can be run in DrRacket:
Welcome to DrRacket, version 8.4 [cs].
Language: Beginning Student with List Abbreviations
The test passed!
>
The next steps in HtDF are template and inventory. For a function with one list
argument the "natural recursion" list template is likely to be appropriate;
(define (fn xs) ;; (Listof X) -> Y ; *template*
(cond ;
[(empty? xs) ... ] #|base case|# ;; Y ;
[else (... #|something|# ;; X Y -> Y ;
(first xs) (fn (rest xs))) ])) ;
With this template the function and the next tests become:
(define (sum-evens-with-prev xs) ;; (Listof Number) -> Number
;; produce total of each even x with its previous element (prev of first is 0)
(cond
[(empty? xs) 0 ] #|base case: from minimal example|#
[else (error "with arguments: " #|something: ?|#
(first xs) (sum-evens-with-prev (rest xs))) ]))
(check-expect (sum-evens-with-prev '(1)) 0)
(check-expect (sum-evens-with-prev '(2)) 2)
These tests fail, but the error messages and purpose statement suggest what is required:
the (... #|something|# from the template has to choose whether to add (first xs):
(define (sum-evens-with-prev xs) ;; (Listof Integer) -> Integer
;; produce total of each even x with its previous element (prev of first is 0)
(cond
[(empty? xs) 0 ]
[else
(if (even? (first xs))
(+ (first xs) (sum-evens-with-prev (rest xs)))
(sum-evens-with-prev (rest xs))) ]))
Now all 3 tests pass! Time for more check-expects (note: careful introduction of
check-expects is a way of clarifying ones understanding of the requirements, and
points one to the code to be added):
(check-expect (sum-evens-with-prev '(1 1)) 0)
(check-expect (sum-evens-with-prev '(1 2)) 3)
Ran 5 tests.
1 of the 5 tests failed.
Check failures:
Actual value 2 differs from 3, the expected value.
sum-evens-with-prev needs the prev value to include in the even? case:
make it available by introducing it as an argument (renaming the function), add
the appropriate arguments to the recursive calls, the function now just calls
sum-evens-and-prev:
(define (sum-evens-and-prev xs prev) ;; (Listof Integer) Integer -> Integer
;; produce total of each even x and prev
(cond
[(empty? xs) 0 ]
[else
(if (even? (first xs))
(+ prev (first xs) (sum-evens-and-prev (rest xs) (first xs)))
(sum-evens-and-prev (rest xs) (first xs))) ]))
(define (sum-evens-with-prev xs) ;; (Listof Integer) -> Integer
;; produce total of each even x with its previous element (prev of first is 0)
(sum-evens-and-prev xs 0))
(just add some more tests, and all is well :)
(check-expect (sum-evens-with-prev '(0 2)) 2)
(check-expect (sum-evens-with-prev '(2 1)) 2)
(check-expect (sum-evens-with-prev '(1 3)) 0)
(check-expect (sum-evens-with-prev '(2 2)) 6)
(check-expect (sum-evens-with-prev '(1 2 3 4)) 10)
(check-expect (sum-evens-with-prev '(1 2 3 3 5 6 6)) 26)
Welcome to DrRacket, version 8.4 [cs].
Language: Beginning Student with List Abbreviations.
All 11 tests passed!
>
The (for/fold) form requires a (values) clause, and it is in that which you would put the conditional form.
Assuming you want only the new list as the return value, you would also want a #:result clause following the iteration variables.
(define loopl
(lambda (l)
(for/fold
([index 0]
[acc '()]
#:result acc)
([i l])
(values [+ index 1]
[append acc
(if (and (> index 0)
(even? i))
(list (+ i (list-ref l (- index 1))))
(list i))]))))
This should give the correct answer.
You almost never want to repeatedly call list-ref in a loop: that makes for horrible performance. Remember that (list-ref l i) takes time proportional to i: in your case you're going to be calling list-ref with the index being, potentially 0, 1, ..., and that going to result in quadratic performance, which is bad.
Instead there's a neat trick if you want to iterate over elements of a list offset by a fixed amount (here 1): iterate over two lists: the list itself and its tail.
In addition you need to check that the first element of the list is not even (because there is no previous element in that case, so this is an error).
Finally I'm not entirely sure what you wanted to return from the function: I'm assuming it's the sum.
(define (accum-even-previouses l)
(unless (not (even? (first l)))
;; if the first elt is even this is an error
(error 'accum-even-previouses "even first elt"))
(for/fold ([accum 0])
([this (in-list (rest l))]
[previous (in-list l)])
(+ accum (if (even? this)
(+ this previous)
0))))

Scheme/Racket: A function which separates a list into two lists of elements that match a certain predicate and those that don't match it

I wish to create a function in Scheme that takes in a predicate and a list of elements, and then outputs two separate lists. One with elements of the original list that MATCH the given predicate, and one with elements that DON'T match it.
The code I have right now I believe should isolate those which match the predicate and output a list of them but the code will not work.
(define tear
(lambda (pred xs)
(cond[(null? xs) '()]
[(list? (car xs))(cons((tear (pred (car xs)))(tear (pred (cdr xs)))))]
[(pred (car xs))(cons((car xs)(tear (pred (cdr xs)))))]
[else tear (pred (cdr xs))])))
(tear number? '(1 2 3 a b c))
The resulting output on my compiler is:
tear: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
arguments...:
#f
context...:
/home/jdoodle.rkt:2:4: tear
Command exited with non-zero status 1
Any help/info that you can give would be much appreciated.
Lets fix your code step by step. Adding indentation and whitespace to make it readable:
(define tear
(lambda (pred xs)
(cond
[(null? xs)
'()]
[(list? (car xs))
(cons ((tear (pred (car xs))) (tear (pred (cdr xs)))))]
[(pred (car xs))
(cons ((car xs) (tear (pred (cdr xs)))))]
[else
tear (pred (cdr xs))])))
(tear number? '(1 2 3 a b c))
The first problem I see is a problem of putting parentheses on the inside (around the arguments) of a function call instead on the outside. You do this with cons and with the recursive calls to tear. For instance in tear (pred (cdr xs)) you should move the first paren to before the function. Remember that parentheses in an expression almost always mean a function call in the shape of (function argument ...).
(cons (A B)) should be rewritten to (cons A B)
(tear (Pred Xs)) should be rewritten to (tear Pred Xs)
tear (Pred Xs) should be rewritten to (tear Pred Xs)
With these fixes your code looks like this:
(define tear
(lambda (pred xs)
(cond
[(null? xs)
'()]
[(list? (car xs))
(cons (tear pred (car xs)) (tear pred (cdr xs)))]
[(pred (car xs))
(cons (car xs) (tear pred (cdr xs)))]
[else
(tear pred (cdr xs))])))
(tear number? '(1 2 3 a b c))
;=> (1 2 3)
(tear number? '(1 2 "not a number" 3 4))
;=> (1 2 3 4)
However, it still does something weird when there's a nested list:
(tear list? (list '(1 2 3) "not a list" '(4 5)))
;=error> (() ())
To be consistent it should put the two lists into a list: ((1 2 3) (4 5)). To do that just remove the second cond case:
(define tear
(lambda (pred xs)
(cond
[(null? xs)
'()]
[(pred (car xs))
(cons (car xs) (tear pred (cdr xs)))]
[else
(tear pred (cdr xs))])))
(tear number? '(1 2 3 a b c))
;=> (1 2 3)
(tear list? (list '(1 2 3) "not a list" '(4 5)))
;=> ((1 2 3) (4 5))
It now seems to do exactly half of what you want. You want it to return two lists: one for elements that passed, and one for the elements that failed. It currently is returning just the first list.
The first thing you should do is document how it returns those two lists. Since there are always exactly two, you can return them as multiple values.
;; tear returns two values:
;; - a list of the elements of `xs` that passed `pred`
;; - a list of the elements of `xs` that failed `pred`
There are two parts of using multiple values: returning them and receiving them. Use (values A B) to return them, and (let-values ([(A B) ....]) ....) to match on a result, like the result of a recursive call.
That means every recursive call like this (f .... (tear ....) ....) should become
(let-values ([(A B) (tear ....)])
(values (f .... A ....)
???))
Applying that to your code:
;; tear returns two values:
;; - a list of the elements of `xs` that passed `pred`
;; - a list of the elements of `xs` that failed `pred`
(define tear
(lambda (pred xs)
(cond
[(null? xs)
(values '()
???)]
[(pred (car xs))
(let-values ([(A B) (tear pred (cdr xs))])
(values (cons (car xs) A)
???))]
[else
(let-values ([(A B) (tear pred (cdr xs))])
(values A
???))])))
Now to fill in the ??? holes, use examples.
(tear number? '()) should return two empty lists: () ()
(tear number? '(1 2)) should return a full list and an empty list: (1 2) ()
(tear number? '(a b)) should return an empty list and a full list: () (a b)
The first example corresponds to the first ??? hole, the second example corresponds to the second hole, and so on.
This tells us that the first hole should be filled in with '(), the second hole should be filled in with B, and the third hole should be filled in with (cons (car xs) B).
(define tear
(lambda (pred xs)
(cond
[(null? xs)
(values '() '())]
[(pred (car xs))
(let-values ([(A B) (tear pred (cdr xs))])
(values (cons (car xs) A)
B))]
[else
(let-values ([(A B) (tear pred (cdr xs))])
(values A
(cons (car xs) B)))])))
(tear number? '(1 2 3 a b c))
;=> (1 2 3)
; (a b c)
(tear list? (list '(1 2 3) "not a list" '(4 5)))
;=> ((1 2 3) (4 5))
; ("not a list")
This is a classic fold use-case. You're aggregating the list into two lists :
(define tear (lambda (pred lst)
(fold-right ; Aggregate over lst
(lambda (elem agg)
(let ((accepted (car agg))
(rejected (cadr agg)))
(if (pred elem)
; Create a new agg by adding the current element to the accepted list
`(,(cons elem accepted) ,rejected)
; Or, if the predicate rejected the element,
; Create a new agg by adding the current element to the rejected list
`(,accepted ,(cons elem rejected)))))
`(() ())
lst)))
So, if you use even? as your predicate, you can get:
> (tear even? `(1 2 3 4 5 6 7 8))
((2 4 6 8) (1 3 5 7))
Here's another way you can do it using continuation-passing style; this puts the recursive call in tail position.
(define (partition p xs (return list))
(if (null? xs)
(return null null)
(partition p
(cdr xs)
(lambda (t f)
(if (p (car xs))
(return (cons (car xs) t)
f)
(return t
(cons (car xs) f)))))))
(partition number? '())
;; => '(() ())
(partition number? '(a 1 b 2 c 3))
;; => '((1 2 3) (a b c))
(partition list? '(1 2 (3 4) (5 6) 7 8))
;; => '(((3 4) (5 6)) (1 2 7 8))
Above, we make use of Racket's default arguments. Below we show how to define partition using a helper function instead
;; procedure above, renamed to partition-helper
(define (partition-helper p xs return)
...)
;; new procedure without optional parameter
(define (partition p xs)
;; call helper with default continuation, list
(partition-helper p xs list))
Comments may help distill some of the style's mysterious nature
;; default continuation is `list`, the list constructor procedure
(define (partition p xs (return list))
(if (null? xs)
;; base case: empty list; return the empty result
(return null null)
;; inductive case: at least one x; recur on the tail...
(partition p
(cdr xs)
;; ...specifying how to continue the pending computation
(lambda (t f)
(if (p (car xs))
;; if predicate passes, cons x onto the t result
(return (cons (car xs) t)
f)
;; otherwise cons x onto the f result
(return t
(cons (car xs) f)))))))
#WillNess asks why we delay evaluating the predicate; I don't have a reason other than I think the readability above is pretty good. We can alter the implementation to check the predicate right away, if we please. The impact here is very subtle. If you don't see it, I encourage you to play pen-and-paper evaluator and compare the two processes to understand it.
;; default continuation is `list`, the list constructor procedure
(define (partition p xs (return list))
(if (null? xs)
;; base case: empty list; return the empty result
(return null null)
;; inductive case: at least one x; recur on the tail...
(partition p
(cdr xs)
;; ...specifying how to continue the pending computation
(if (p (car xs))
(lambda (t f)
;; if predicate passes, cons x onto the t result
(return (cons (car xs) t)
f))
(lambda (t f)
;; otherwise cons x onto the f result
(return t
(cons (car xs) f)))))))

cons function does not accept my input

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
Note that when you link to cons in #lang racket it is not the same as cons in other languages, like #lang pl. Same names functions in different languages can be the same but often are they different. Eg. cons and friends in #!r6rs are mapped from mcons and friends in #lang racket
I have no idea what #lang pl is but it seems like it is typed in some way. I assume that when you say you do:
(cons (Listof Number) (Listof (Listof Number)))
You mean that you do:
(cons a b)
Where a is of type (Listof Number) and b is of type (Listof (Listof Number)). I notice that the type of the second is Listof whatever was type on the first. In your trial the types don't have that relationship but the other way around. It's likely that you have switched the arguments and that the correct code for the acc is:
(cons (get-min&max-from-mixed-list (first lol)) acc)
I assume type specification of cons might require the second list to be lists of the same type as the type of the first argument. For the untyped languages like Scheme and #lang racket you can have any type in the two arguments.
#lang racket
(cons 3 #f) ; ==> (3 . #f)

Finding the occurence element in the list in racket

Assume (list "apple" "orange" "apple" "grape" "orange")and produce (list (list 2 "apple") (list 2 "orange") (list 1 "grape")).
The most common fruit will occur first in the produced list.
In the case of ties, order the tied pairs with the fruit in increasing alphabetical order.
use abstract list function such as map,filter, foldr and quicksort in local. no recursion.
i'm not sure how to do it without recursion.
i wrote like this:
(define (function list)
(cond
[(empty? list) empty]
[else
(local
(define (helper1 a b)
(cond
[(equal? a b) a]
[else b]))
(define T (foldr helper1 (first list) (rest list)))
(define (count a)
(cond
[(equal? a T) true]
[else false]))
(define new-list (quicksort (length (filter count list)) >))]
The most efficient way is to use a (mutable) hash table:
(define (count-by-type lst)
; create hash
(define h (make-hash))
; update hash, creating entries if needed, otherwise adding 1 to existing entry
(map (lambda (e) (hash-update! h e add1 0)) lst)
; create list of (count key) elements from hash and sort accordingly
(sort (map (lambda (e) (list (cdr e) (car e))) (hash->list h))
(lambda (x y) (or (> (car x) (car y))
(and (= (car x) (car y)) (string<? (cadr x) (cadr y)))))))
testing:
> (count-by-type (list "apple" "orange" "apple" "grape" "orange"))
'((2 "apple") (2 "orange") (1 "grape"))
I just rehashed my own answer from a previous question. This seems to be a similar assignment, but without struct
Using a hash you could do it with only one pass through the unsorted list, then produced a list that then was sorted with a special <-function that sorts by count, then fruit.
These hints are for a functional solution. First sort the argument (sort list-of-fruits string>?). that the in descending order and oposite of your result. .
Given the list has at least one element:
(let rec ((cur (car sorted-fruits)) (cnt 1) (lst (cdr sorted-fruits)) (acc '()))
(cond ((equal? cur (car lst)) (rec cur (add1 cnt) (cdr lst) acc))
(else (rec (car lst) 1 (cdr lst) (cons (list cnt cur) acc)))))
This will produce a list in ascending order with counts.
If you sort again:
(sort list-of-counts-and-fruit (lambda (x y) (>= (car x) (car y)))
sort in Racket is stable. That means if you have two with equal counts in the list they will end up in their original order. The original order was the ascending animal order so the result is ordered by count descending, then name ascending.
I guess your procedure can be made by chaining these together, perhaps using let to store intermediates to make expressions shorter and more readable.

I need to merge two lists in Scheme, taking one element from each one

This is what I've written:
(: mmm : (Listof Any) (Listof Any) -> (Listof Any))
(define (mmm list1 list2)
(cond [(or (null? list1) (null? list2)) null]
(and (cons (first list1) (first list2)) (mmm (rest list1) (rest list2)))))
I'll give you an example:
list1: a b c
list2: 1 2 3
answer: ((a 1) (b 2) (c 3))
edited
they both same size
If you are allowed to use map, you can use:
(define (mmm lst1 lst2) (map (lambda (x y) (list x y)) lst1 lst2))
There are a couple of problems with your code:
The base case might or might not be right - if the input lists are guaranteed to be of the same size, then it's fine. If they can be of different size see #Sylwester's answer for advice
The second condition should start with an else (because it's the last one and the conditions are mutually exclusive), not with and
You are not building the output list correctly. The new element to be added is a list with the first element of both input lists, and that must be consed to the result of calling the recursion
To fix them, try this - assuming input lists of equal length:
(define (mmm list1 list2)
(cond [(or (null? list1) (null? list2)) null]
[else (cons (list (first list1) (first list2))
(mmm (rest list1) (rest list2)))]))
Now the procedure works as expected:
(mmm '(a b c ) '(1 2 3))
=> '((a 1) (b 2) (c 3))
If you know the lists are the same size, then it becomes simply:
(define (mmm list1 list2) (map list list1 list2))
Note: Replace map list with map cons if that is what you really want. Your code used cons but your result example suggests that list is what you want.