Scheme Pair's help - scheme

How can you go from (5 . 2) to (5 2) ??

Is this what you're looking for?
(define (pair-to-list pp)
(list (car pp) (cdr pp)))
(pair-to-list '(5 . 2))
(pair-to-list (cons 5 2))

Related

run lenght encode symbols in list

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

contract violation expected: number?-Scheme

I am new to racket and scheme and I am attempting to map the combination of a list to the plus funtion which take each combination of the list and add them together like follows:
;The returned combinations
((1 3) (2 3) (1 4) (2 4) (3 4) (1 5) (2 5) (3 5) (4 5) (1 6) (2 6) (3 6) (4 6) (5 6) (1 2) (2 2) (3 2) (4 2) (5 2) (6 2))
; expected results
((2) (5) (5).....)
Unfortunately I am receiving the contract violation expected error from the following code:
;list of numbers
(define l(list 1 2 3 4 5 6 2))
(define (plus l)
(+(car l)(cdr l)))
(map (plus(combinations l 2)))
There are a couple of additional issues with your code, besides the error pointed out by #DanD. This should fix them:
(define lst (list 1 2 3 4 5 6 2))
(define (plus lst)
(list (+ (car lst) (cadr lst))))
(map plus (combinations lst 2))
It's not a good idea to call a variable l, at first sight I thought it was a 1. Better call it lst (not list, please - that's a built-in procedure)
In the expected output, weren't you supposed to produce a list of lists? add a call to list to plus
You're not passing plus in the way that map expects it
Do notice the proper way to indent and format your code, it'll help you in finding bugs
You want (cadr l). Not (cdr l) in your plus function:
(define (plus l)
(+ (car l) (cadr l)))
Where x is (cons 1 (cons 2 '())):
(car x) => 1
(cdr x) => (cons 2 '())
(cadr x) == (car (cdr x)) => 2

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))))

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))

get 7 not (7) out of the list?

Here's my code:
(define step1_list1 '(1 3 (5 7) 9))
(car (cdr (cdr (step1_list1))))
(define step1_list2 '((7)))
(car (step1_list2))
(define step1_list3 '(1 (2 (3 (4 (5 (6 7)))))))
(car (cdr (cdr (cdr (cdr (cdr step1_list3))))))
))
Running this code causes an error:
(1 3 (5 7) 9) is not applicable
What is the problem?
Start small.
(define mylist '(1 2 3))
(display mylist)
(display (car mylist))
(display (car (mylist)))
Run each of those in turn, and see what you get at each step. Once you understand why you get the output you do, then you should be able to fix the code in your question.
In Scheme, (non-quoted) parentheses mean function application. So (car (step1_list2)) tries to execute step1_list2 as a procedure (and then take the car of the result). Instead, you want:
(car step1_list2)

Resources