run lenght encode symbols in list - scheme

I would like to ask you for help to solve my task with printing of result of code below. Input is: (run-length-encode '(1 1 2 2 3 3 4 4 a a b b)) and it returns ((2 . 1) (2 . 2) (2 . 3) (2 . 4)) but i would like to have output like (2 1 2 2 2 3 2 4 2 a 1 b). What i am doing wrong?
(define (run-length-encode lst)
(define (rle val-lst cur-val cur-cnt acc)
(if (pair? val-lst)
(let ((new-val (car val-lst)))
(if (eq? new-val cur-val)
(rle (cdr val-lst) cur-val (+ cur-cnt 1) acc)
(rle (cdr val-lst) new-val 1 (cons (cons cur-cnt cur-val) acc))))
(cons (cons cur-cnt cur-val) acc)))
(if (pair? lst)
(reverse (rle (cdr lst) (car lst) 1 '()))
'()))
Thank you for help,
Jan

Related

Scheme Double All List Values and Write Mapping Function

I have a question on a homework assignment that is as follows:
I have the first part (double-list-elements) complete
(define (double-list-elements s)
(if (null? s)
'()
(cons (* 2 (car s))
(double-list-elements (cdr s)))))
but I honestly have absolutely no idea how to proceed with double-list-elements-one. Any pointers would be greatly appreciated.
Close But No Cigar:
(define (double-list-elements s)
(if (null? s)
'()
(cons (* 2 (car s))
(double-list-elements (cdr s)))))
(define (custom-map proc lst)
(if (null? lst)
'()
(cons (proc lst)
(custom-map proc (cdr lst)))))
(define (double-list-elements-one func s)
(custom-map double-list-elements s))
(double-list-elements-one double-list-elements '(1 2 3 4 5 6))
Output = (list (list 2 4 6 8 10 12) (list 4 6 8 10 12) (list 6 8 10 12) (list 8 10 12) (list 10 12) (list 12))
I need to pass double-list-elements to double-list-elements-one as a parameter as well as the list.
You just have to write your own map, it's very very similar to what you already have, simply pass the part that changes as a procedure parameter, and invoke it on the right place:
(define (mymap proc lst)
(if (null? lst)
'()
(cons <invoke proc on current element>
(mymap proc (cdr lst)))))
(define (double-list-elements-one s)
(mymap <pass a proper lambda> s))
(double-list-elements-one '(1 2 3 4 5))
=> '(2 4 6 8 10)

Sorting list of lists by their first element in scheme

I'm working on sorting a list of lists by their first element for example
(sort (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 1))))
expected output => ('(1 1) '(2 1 6 7) '(4 3 1 2 4 5))
The algorithm I used is bubble sort. And I modified it to deal with lists. However, the code doesn't compile. The error is
mcar: contract violation
expected: mpair?
given: 4
Can someone correct my code and explain it. Thank you
(define (bubble L)
(if (null? (cdr L))
L
(if (< (car (car L)) (car (cadr L)))
(list (car L)
(bubble (car (cdr L))))
(list (cadr L)
(bubble (cons (car (car L)) (car (cddr L))))))))
(define (bubble-sort N L)
(cond ((= N 1) (bubble L))
(else
(bubble-sort (- N 1) (bubble L)))))
(define (bubble-set-up L)
(bubble-sort (length L) L))
(define t3 (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 2 3) '(1 1)))
(bubble-set-up t3)
How about (sort (lambda (x y)(< (car x)(car y))) <YOUR_LIST>)?
I have fixed a few mistakes. There is at least one mistake left.
Consider the case where L only contains one element.
#lang r5rs
(define (bubble L)
(if (null? (cdr L))
L
(if (< (car (car L)) (car (cadr L)))
(cons (car L)
(bubble (cdr L)))
(cons (cadr L)
(bubble (cons (car L) (cddr L)))))))
(define (bubble-sort N L)
(cond ((= N 1) (bubble L))
(else
(bubble-sort (- N 1) (bubble L)))))
(define (bubble-set-up L)
(bubble-sort (length L) L))
(define t3 (list '(2 1 6 7) '(4 3 1 2 4 5) '(1 2 3) '(1 1)))
(display (bubble-set-up t3))
(newline)

Infix to Prefix conversion scheme

I'm trying to figure out how to convert an infix expression to prefix in Scheme.
I found this post which does what I want but in the opposite direction. What changes when going from infix->prefix instead of prefix->infix?
Edit: I forgot to mention I need to account and handle for variables. For example the input
'(2 + 3 * a ^ 5 + b)
It's rather trivial to modify the algorithm you link to:
(define (infix->prefix lst)
(cond
((list? lst)
(unless (= 3 (length lst)) (error "not 3 elements"))
(let ((operand1 (car lst))
(operator (cadr lst))
(operand2 (caddr lst)))
(list operator
(infix->prefix operand1)
(infix->prefix operand2))))
(else lst)))
Testing:
> (infix->prefix '(1 + 2))
'(+ 1 2)
> (infix->prefix '(1 + (2 * 3)))
'(+ 1 (* 2 3))
> (infix->prefix '((1 / 4) + (2 * 3)))
'(+ (/ 1 4) (* 2 3))
This is not a general algorithm though; if you need something more elaborate then please show some examples of conversions you need to do.
EDIT Here's an example code that works for longer expressions but doesn't implement operator precedence:
(define (infix->prefix lst)
(if (list? lst)
(if (null? (cdr lst))
; list with one element -> return element
(infix->prefix (car lst))
; list with more than one element
(list (cadr lst)
(infix->prefix (car lst))
(infix->prefix (cddr lst))))
; not a list -> return element
lst))
Testing:
> (infix->prefix '(2 + 3 * a ^ 5 + b))
'(+ 2 (* 3 (^ a (+ 5 b))))

(Scheme) How do I add 2 lists together that are different sizes

I'm completely new to scheme and I'm having trouble trying to add 2 lists of different sizes. I was wondering how do I add 2 lists of different sizes together correctly. In my code I compared the values and append '(0) to the shorter list so that they can get equal sizes, but even after doing that I can not use map to add the 2 lists. I get an error code after running the program. The results I should be getting is '(2 4 5 4). Could anyone help me out? Thanks.
#lang racket
(define (add lst1 lst2)
(cond [(< (length lst1) (length lst2)) (cons (append lst1 '(0)))]
[else lst1])
(cond
((and (null? lst1)(null? lst2)) null)
(else
(map + lst1 lst2))))
;;Result should be '(2 4 6 4)
(add '(1 2 3) '(1 2 3 4))
ERROR:
cons: arity mismatch;
the expected number of arguments does not match the given number
expected: 2
given: 1
arguments...:
'(1 2 3 0)
The problem with your code is that there are two cond expressions one after the other - both will execute, but only the result of the second one will be returned - in other words, the code is not doing what you think it's doing. Now, to solve this problem it'll be easier if we split the solution in two parts (in general, that's a good strategy!). Try this:
(define (fill-zeroes lst n)
(append lst (make-list (abs n) 0)))
(define (add lst1 lst2)
(let ((diff (- (length lst1) (length lst2))))
(cond [(< diff 0)
(map + (fill-zeroes lst1 diff) lst2)]
[(> diff 0)
(map + lst1 (fill-zeroes lst2 diff))]
[else (map + lst1 lst2)])))
Explanation:
The fill-zeroes procedure takes care of filling the tail of a list with a given number of zeroes
The add procedure is in charge of determining which list needs to be filled, and when both lists have the right size performs the actual addition
It works as expected for any combination of list lengths:
(add '(1 2 3 4) '(1 2 3))
=> '(2 4 6 4)
(add '(1 2 3) '(1 2 3 4))
=> '(2 4 6 4)
(add '(1 2 3 0) '(1 2 3 4))
=> '(2 4 6 4)
Similar to Oscar's, slighty shorter:
(define (fill0 lst len)
(append lst (make-list (- len (length lst)) 0)))
(define (add lst1 lst2)
(let ((maxlen (max (length lst1) (length lst2))))
(map + (fill0 lst1 maxlen) (fill0 lst2 maxlen))))
or, for fun, the other way round:
(define (add lst1 lst2)
(let ((minlen (min (length lst1) (length lst2))))
(append
(map + (take lst1 minlen) (take lst2 minlen))
(drop lst1 minlen)
(drop lst2 minlen))))
There's no need to pre-compute the lengths of the lists and add zeroes to the end of one or the other of the lists. Here we solve the problem with a simple recursion:
(define (add xs ys)
(cond ((and (pair? xs) (pair? ys))
(cons (+ (car xs) (car ys)) (add (cdr xs) (cdr ys))))
((pair? xs) (cons (car xs) (add (cdr xs) ys)))
((pair? ys) (cons (car ys) (add xs (cdr ys))))
(else '())))
That works for all of Oscar's tests:
> (add '(1 2 3 4) '(1 2 3))
(2 4 6 4)
> (add '(1 2 3) '(1 2 3 4))
(2 4 6 4)
> (add '(1 2 3 0) '(1 2 3 4))
(2 4 6 4)
If you like, you can write that using a named-let and get the same results:
(define (add xs ys)
(let loop ((xs xs) (ys ys) (zs '()))
(cond ((and (pair? xs) (pair? ys))
(loop (cdr xs) (cdr ys) (cons (+ (car xs) (car ys)) zs)))
((pair? xs) (loop (cdr xs) ys (cons (car xs) zs)))
((pair? ys) (loop xs (cdr ys) (cons (car ys) zs)))
(else (reverse zs)))))
Have fun!
A yet simpler version.
(define (add x y)
(cond ((and (pair? x) (pair? y))
(cons (+ (car x) (car y))
(add (cdr x) (cdr y))))
((pair? x) x)
(else y)))

How to count and remove element at the same time in the list in Scheme

I have two procedures, one for counting an element in the list and the other one for removing the same element from the same list. What should I do for counting and removing at the same time? I am trying it for long time but nothing is working. I work with this list: (list 1 2 3 2 1 2 3), finally it should be like: ((1 . 2) (2 . 3) (3 . 2)). The first number of pair is an element and second number of pair is sum of first pair's number from all list.
My try:
1) it works only with counting and result is: ((1 . 2) (2 . 3) (3 . 2) (2 . 2) (1 . 1) (2 . 1) (3 . 1))
2) it works only with removing and result is: ((1 . 2) 2 3 2 2 3)
Where is the problem?
This is for counting:
(define count-occurrences
(lambda (x ls)
(cond
[(memq x ls) =>
(lambda (ls)
(+ (count-occurrences x (cdr ls)) 1))]
[else 0])))
(count-occurrences '2 (list 1 2 3 2 1 2 3)) -> 3
This is for removing:
(define (remove-el p s)
(cond ((null? s) '())
((equal? p (car s)) (remove-el p (cdr s)))
(else (cons (car s) (remove-el p (cdr s))))))
(remove-el '2 (list 1 2 3 2 1 2 3)) -> (1 3 1 3)
Just return the count and the removed list at once. I call this routine
count-remove. (Pardon to all schemers for not idiomatic or efficient style)
(define (count-remove ls x)
(letrec ([loop (lambda (count l removed)
(cond
[(eq? l '()) (list count removed)]
[(eq? (car l) x) (loop (+ 1 count) (cdr l) removed)]
[else (loop count (cdr l) (cons (car l) removed))]))])
(loop 0 ls '())))
(define (count-map ls)
(cond
[(eq? ls '()) '()]
[else
(letrec ([elem (car ls)]
[cr (count-remove ls elem)])
(cons (cons elem (car cr)) (count-map (cadr cr))))]))
Here is some usage:
(count-map '(1 1 2 3 2))
((1 . 2) (2 . 2) (3 . 1))

Resources