can someone help me debug my scheme code - list

I want to write a simple function (highest L k), which take a list and interger K>0, return a new list with k highest numbers.
(highest '(6 7 8 5 3 2) 3) give (6 7 8)
(define (highest L k)
(if (= k 0)
'()
(cons (highesthelper (car L) L)
((highest (remove (highesthelper (car L) L) L) (- k 1))))))
(define (remove E L)
(cond
((null? L)'())
((= E (car L)) (cdr L))
(else (cons (car L) (remove E (cdr L))))))
(define (highesthelper Hi L)
(cond
((null? L) Hi)
((> Hi (car L)) highesthelper Hi (cdr L))
(else (highesthelper (car L) (cdr L)))))
(highest '(1 7 4 5 3) 2)
I can run the helper and remove function without problem, but the highest function give me bugs, can someone help me?
Thanks

I fixed a few simple mistakes. Note in particular the (> Hi (car L)) case of highesthelper.
(define (highest L k)
(if (= k 0)
'()
(cons (highesthelper (car L) L)
(highest (remove (highesthelper (car L) L) L) (- k 1)))))
(define (remove E L)
(cond
((null? L) '())
((= E (car L)) (cdr L))
(else (cons (car L) (remove E (cdr L))))))
(define (highesthelper Hi L)
(cond
((null? L) Hi)
((> Hi (car L)) (highesthelper Hi (cdr L)))
(else (highesthelper (car L) (cdr L)))))
(highest '(1 7 4 5 3) 2)

Related

DrRacket - creating a list within a list using minimal built in functions

I need a function that will do this:
Odd Length list
Input '(1 2 3 4 5) = '(1 (2 (3) 4) 5)
Even length list
Input '(1 2 3 4) = '(1 (2 () 3) 4)
It needs to use very minimal built in functions. I have spent hours trying to figure this out and I am completely out of ideas at this point.
Here is what I have:
(define (listInList L)
(define length (listLength L))
(define L2 (listInListHelper length L '() '()))
(define L3 (listInListHelper (- length 2) L L2 '()))
L3
)
(define (listInListHelper N L NL)
(cond
((= N 0) '()
((= N 1) (cons (list (car L)) NL))
(else (cons (cons (car L) (list (lastItem L))) NL)
(remove 1 L)))
)
)
(define (lastItem L)
(if (null? (cdr L))(car L)
(lastItem (cdr L)))
)
(define (remove N L)
(cond ((eq? N 0) (cdr L))
(else (cons (car L) (remove (- N 1)(cdr L))))))
This would be one way to do it, you need to tell me if it's minimal enough:
(define (f lst)
(define (helper lst rlst half)
(cond
((= half 0 ) null)
((= half 1/2) (list (car lst)))
(else (list (car lst)
(helper (cdr lst) (cdr rlst) (sub1 half))
(car rlst)))))
(helper lst (reverse lst) (/ (length lst) 2)))
testing:
> (f '(1 2 3 4 5))
'(1 (2 (3) 4) 5)
> (f '(1 2 3 4))
'(1 (2 () 3) 4)

How to make pairs from a numeric list based on cardinality?

I have a list '(1 2 1 1 4 5) and want output list as '((1 3)(2 1)(4 1)(5 1)). I have written a small code but I am stuck with how to calculate the cardinality for each number and then put it as pair in list. Can anyone please look at my code and give some ideas?
(define set2bags
(lambda (randlist)
(cond ((null? randlist) '())
(else
(sort randlist)
(makepairs randlist)))))
(define makepairs
(lambda (inlist)
(let ((x 0)) ((newlist '()))
(cond ((zero? (car inlist)) '())
(else
(eq? (car inlist)(car (cdr inlist)))
(+ x 1)
(makepairs (cdr inlist))
(append newlist (cons (car inlist) x)))))))
Your current solution is incorrect - it doesn't even compile. Let's start again from scratch, using a named let for traversing the input list:
(define set2bags
(lambda (randlist)
(cond ((null? randlist) '())
(else (makepairs (sort randlist >))))))
(define makepairs
(lambda (inlist)
(let loop ((lst inlist)
(prv (car inlist))
(num 0)
(acc '()))
(cond ((null? lst)
(cons (list prv num) acc))
((= (car lst) prv)
(loop (cdr lst) prv (add1 num) acc))
(else
(loop (cdr lst) (car lst) 1 (cons (list prv num) acc)))))))
Now it works as expected:
(set2bags '(1 2 1 1 4 5))
=> '((1 3) (2 1) (4 1) (5 1))
The trick is keeping a counter for the cardinality (I called it num), and incrementing it as long as the same previous element (I named it prv) equals the current element. Whenever we find a different element, we add a new pair to the output list (called acc) and reset the previous element and the counter.
Your code is fairly hard to read without proper formating.
I notice a two branch cond, which is easier to read as an if.
In your else clause of set2bags, you call (sort randlist) but leave it as is. You actually want to use this in the next s-expression (makepairs (sort randlist))
So far a pretty good idea.
Now in makepairs you should have better abstraction, say let variables like-first and unlike-first. If the inlist is null, then the function should be the null list, else it's the pair with the car being the list of the car of like-first and the length of like-first and the cdr being the result of calling makepairs on the unlike-first list
(define (makepairs inlist)
(let ((like-first (filter (lambda (x) (equal? x (car inlist)) inlist))
(unlike-first (filter (lambda (x) (not (equal? x (car inlist))) inlist)))
(if (null? inlist)
'()
(cons (list (car inlist) (length like-first)) (makepairs unlike-first)))))
more effecient version
(define (makepairs inlist)
(if (null? inlist)
'()
(let loop ((firsts (list (car inlist)))
(but-firsts (cdr inlist)))
(if (or (null? but-firsts)
(not (equal? (car firsts) (car but-firsts))))
(cons (list (car firsts) (length firsts))
(makepairs but-firsts))
(loop (cons (car but-firsts) firsts) (cdr but-firsts))))))
]=> (makepairs (list 1 1 1 2 4 5))
;Value 17: ((1 3) (2 1) (4 1) (5 1))
If you have your own implementation of sort, say a mergesort you could write this right into the merge part for the best effeciency.
(define (set2bags lst)
(mergesort2bags lst <))
(define (mergesort2bags lst pred)
(let* ((halves (divide-evenly lst))
(first-half (car halves))
(other-half (cadr halves)))
(cond ((null? lst) '())
((null? (cdr lst)) (list (list (car lst) 1)))
(else
(merge-bags
(mergesort2bags first-half pred)
(mergesort2bags other-half pred)
pred)))))
(define (divide-evenly lst)
(let loop
((to-go lst)
(L1 '())
(l2 '()))
(if (null? to-go)
(list L1 L2)
(loop (cdr to-go) (cons (car to-go) L2) L1))))
(define (merge-bags L1 L2 pred)
(cond ((null? L1) L2)
((null? L2) L1)
((pred (caar L1) (caar L2))
(cons (car L1) (merge-bags (cdr L1) L2 pred)))
((equal? (caar L1) (caar L2))
(cons (list (caar L1) (+ (cadar L1) (cadar L2)))
(merge-bags (cdr L1) (cdr L2) pred)))
(else (cons (car L2) (merge-bags L1 (cdr L2) pred)))))
(mergesort2bags (list 1 2 1 1 4 5) <)
;Value 46: ((1 3) (2 1) (4 1) (5 1))
I'm thinking for very large datasets with a lot of repetition this method would pay off.

Determining the largest missing number in a list

The argument is a list and I am trying to extract the largest missing number from the list.
I have sorted the list in the function select, but I am having a hard time getting the function select to return the sorted list. When the list returns from sorting it, I am subtracting the highest and second highest to determine the difference and checking to see if the difference is greater than 1.
Could someone let me know how to return the sorted list from Selected to LargestGap?
(define (LargestGap L)
(cond ( (null? L) '() )
; ( car (select L))
( (> (- (car(cdr(select L))) (car(select L))) 0) (LargestGap (cdr(select L))))
(- (car(select L)) 1)))
(define (select L)
(cond ( (null? L) '() )
( else
(cons (Largest L (car L))
(select (delete L (Largest L (car L))))))))
(define (delete L A)
(cond ( (null? L) '() )
( (= (car L) A) (cdr L))
(else (cons (car L)(delete (cdr L) A)))))
(define (Largest L A)
(cond ( (null? L) A)
( (> (car L) A) (Largest (cdr L)(car L)))
(else (Largest (cdr L) A ))))
I am not sure if I got your question correctly. I guess you want to avoid calling select function many times in LargestGap function. If this is what your question is then:
Make another function:
(define (get-largest-gap L)
(LargestGap (select L)))
Make function LargestGap such that is accepts sorted list:
(define (LargestGap L)
(cond ( (null? L) '() )
( (> (- (cadr L) (car L)) 0) (LargestGap (cdr L)))
(- (car L) 1)))

Sum of even in Scheme

This is my first experience with Scheme. I have a list with integers and I wanna get the sum of all even number in list.
; sum_even
(define (sum_even l)
(if (null? l) l
(cond ((even? (car l)) 0)
((not(even? (car l))) (car l)))
(+ (sum_even (car l) (sum_even(cdr l))))))
(sum_even '(2 3 4))
(define (sum_even l)
(cond ((null? l) 0)
((even? (car l)) (+ (car l) (sum_even (cdr l))))
(else (sum_even (cdr l)))))
Not tested
You're not exactly asking a question. Are you checking if your solution is correct or looking for an alternate solution?
You can also implement it as follows via
(apply + (filter even? lst))
edit: If, as you mentioned, you can't use filter, this solution will work and is tail-recursive:
(define (sum-even lst)
(let loop ((only-evens lst) (sum 0))
(cond
((null? only-evens) sum)
((even? (car only-evens))
(loop (cdr only-evens) (+ (car only-evens) sum)))
(else (loop (cdr only-evens) sum)))))
(define (sum-even xs)
(foldl (lambda (e acc)
(if (even? e)
(+ e acc)
acc))
0
xs))
Example:
> (sum-even (list 1 2 3 4 5 6 6))
18
Here is another one with higher order functions and no explicit recursion:
(use srfi-1)
(define (sum-even ls) (fold + 0 (filter even? ls)))
Consider using the built-in filter function. For example:
(filter even? l)
will return a list of even numbers in the list l. There are lots of ways to sum numbers in a list (example taken from http://groups.engin.umd.umich.edu/CIS/course.des/cis400/scheme/listsum.htm):
;
; List Sum
; By Jerry Smith
;
(define (list-sum lst)
(cond
((null? lst)
0)
((pair? (car lst))
(+(list-sum (car lst)) (list-sum (cdr lst))))
(else
(+ (car lst) (list-sum (cdr lst))))))

Only by using cons car cdr

How do I get elements from 2nd to 7th from a list, using only the following three functions:
cons
car
cdr
Example;
> (two-to-seven (list 8 9 5 1 0 3 6 2 4))
> (9 5 1 0 3 6)
Thanks.
> (define (t2s xs)
(cons (car (cdr xs)) (cons (car (cdr (cdr xs))) (cons (car (cdr (cdr (cdr xs)))) (cons (car (cdr (cdr (cdr (cdr xs))))) (cons (car (cdr (cdr (cdr (cdr (cdr xs)))))) (cons (car (cdr (cdr (cdr (cdr (cdr (cdr xs))))))) (list))))))))
> (t2s (list 8 2 5 4 0 3 6 1 1))
(2 5 4 0 3 6)
My solution. You might want to initialize the acumulator ( acum ) with 1, and/or use >= and <=. You provided no output.
(define (test-get start stop tlst)
(define acum 0)
(define (get-elems lst)
(cond ((null? lst) empty)
((and (symbol? (car lst))
(< acum stop)
(> acum start))
(set! acum (+ 1 acum))
(cons (car lst) (get-elems (cdr lst))))
((symbol? (car lst))
(set! acum (+ 1 acum))
(get-elems (cdr lst)))
(else (append (get-elems (car lst)) (get-elems (cdr lst))))))
(get-elems tlst))
Sample output
> (test-get 0 3 '(a (b) (c (d)) e (((f))) (h (i (j))) (k l)))
(b c)
> (test-get 2 6 '(a (b) (c (d)) e (((f))) (h (i (j))) (k l)))
(d e f)
> (test-get 2 7 '(a (b) (c (d)) e (((f))) (h (i (j))) (k l)))
(d e f h)
And if you're bothered by the append showing there you could replace it with your own using cons, cdr, car
(define (my-append l1 l2)
(if (null? l1)
l2
(cons (car l1) (my-append (cdr l1) l2))))
To also get rid of the set! so we'll be more in the bounds of functional programming (not tested):
(define (test-get start stop tlst)
(define (liniarize lst)
(cond ((null? lst) empty)
((symbol? (car lst)) (cons (car lst) (liniarize (cdr lst))))
(else (my-append (liniarize (car lst)) (liniarize (cdr lst))))))
(define (take-elems lst acum)
(cond ((null? lst) empty)
((and (< acum stop)
(> acum start)) (cons (car lst) (take-elems (cdr lst) (+ 1 acum))))
(else (take-elems lst (+ 1 acum)))))
(take-elems (liniarize tlst) 0))