[racket-users] do loops
I try to fill a binary tree with 5 random numbers, avoiding duplicates. Is there a more elegant way than this (using "Iterations and Comprehensions")? (define tree4 (do ([x (random 10) (random 10)] [c #f (contains? tree x)] [tree null (if c tree (insert tree x))] [i 0 (if c i (add1 i))]) [(>= i 5) tree])) Thanks! -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] do loops
Right, but evaluating tree-size is O(n), even with a binary search tree. Doing this on each insertion is slow. The following is a possible solution without "do", and it only requires O(log n) on a (balanced) search tree: (define tree5 (let loop ([tree null] [n 5]) (define x (random 10)) (cond [(zero? n) tree] [(contains? tree x) (loop tree n)] [else (loop (insert tree x) (sub1 n))]))) But I thought there must be a more elegant solution using iterations and comprehensions. Thank you anyway! -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
Re: [racket-users] do loops
> it only requires O(log n) on a (balanced) search tree: sorry, O(log n) for conttains? and O(n log n) in total -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.
[racket-users] Removing duplicates from a list while still maintaining order in Dr. Racket.
In Beginning Student with List Abbreviations I am struggling to come up with functions to remove duplicates from a list while maintaining the order of the list. one function to remove duplicates from the left, i.e. 1 2 1 3 2 4 5 -> 1 2 3 4 5 and one from the right. i.e. 1 2 1 3 2 4 5 -> 1 3 2 4 5 What are the functions for removing duplicates from each left and right side? If you need to, use helper functions and append, cond, cons, equal?, etc but not using reverse or any built-in functions. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/6049507b-094e-43c9-9dda-28237fcb57d8n%40googlegroups.com.
[racket-users] Removing duplicates in a list in Dr. Racket
In Beginning Student with List Abbreviations I am struggling to come up with functions to remove duplicates from a list while maintaining the order of the list. one function to remove duplicates from the left, i.e. 1 2 1 3 2 4 5 -> 1 2 3 4 5 and one from the right. i.e. 1 2 1 3 2 4 5 -> 1 3 2 4 5 What are the functions for removing duplicates from each left and right side? If you need to, use helper functions and append, cond, cons, equal?, etc but not using reverse or any built-in functions. For example, with unique-left I came up with the below code but it only removes any consecutive duplicates e. g. (list 2 3 4 4 5 3) -> (list 2 3 4 5 3) ;(define (unique-left lst) ; (cond ; [(empty? lst) 0] ;[(empty? (rest lst)) lst] ;[else ; (if (equal? (first lst) (first (rest lst))) ; (unique-left (rest lst)) ;(cons (first lst) (unique-left (rest lst])) So the code does not get me the unique-left like I want to show as the top example. -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/4739e5ce-d328-4d09-859a-bacac2b1989fn%40googlegroups.com.
[racket-users] Making my own helper function
I am in a course where I have to use beginning student with list abbreviation in Racket to do assignments. Below is my code for unique-right, which is supposed to return a list which has only the right most occurrences of the elements of list. The restriction in doing the asisngment is that I cannot use any of the built-in functions such as append or member. I'd need to re name a function and define it as I was doing below. For example, in the place where I need to use member? I renamed it as mymember. Now what I am trying to do is define the helper function mymember. I know that that bold part is incorrect but I have no idea at the moment how to make the code correct. ; unique-right ; List -> List ; returns a list which has only the right most occurrences of the elements of lst (define (unique-right lst) (cond [(empty? lst) lst] [else (if (mymember? (first lst) (rest lst)) (unique-right (rest lst)) (cons (first lst) (unique-right (rest lst])) *; mymember* *; List -> List* *; returns a list that has all of the elements of lst plus a non-occurring elt* *(define (mymember elt lst)* * (cond* *[(empty? lst) lst]* *[else (if (equal? elt (first lst))* * lst* * (cons (mymember elt (rest lst)))]))* -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/f055030f-d497-4201-a969-3e69cc547c39o%40googlegroups.com.