Add even items in list - scheme

I'd like to add the even items in a list and have the following algorithm I wrote to achieve the objective.
The error I am getting is:
+: expects type <number> as 2nd argument, given: #<void>; other arguments were: 4
The code:
(define (mylength alist cnt)
(if (null? alist)
0
(if (= (modulo cnt 2) 0)(+ (car alist) (mylength (cdr alist) (+ cnt 1)))))
(if (= (modulo cnt 2) 1)(mylength (cdr alist) (+ cnt 1))))
Could you please advise on the
i) error
ii) logic of the algorithm
Thanks!

First, indent your code properly:
(define (mylength alist cnt)
(if (null? alist) ;
0 ; this section is wasted because
(if (= (modulo cnt 2) 0) ; it's not the last expression
(+ (car alist) (mylength (cdr alist) (+ cnt 1))))) ;
(if (= (modulo cnt 2) 1) ; this if is the last expression, but
(mylength (cdr alist) (+ cnt 1)))) ; if it's false you get #<void>
You shouldn't have if expressions that don't have both the true and false branches. You have to remember that this isn't C, and anything that's not the last expression will be run and then thrown away.
Combine the last two if statements into one if statement:
(define (mylength alist cnt)
(if (null? alist)
0
(if (= (modulo cnt 2) 0)
(+ (car alist) (mylength (cdr alist) (+ cnt 1)))
(mylength (cdr alist) (+ cnt 1)))))
Edit: When I wrote "anything that's not the last expression will be run and then thrown away", I meant:
(begin
(+ 2 2)
(+ 4 1)
(+ 1 0)
(+ 1 1)) => 2
(let ((x 5))
(add1 x)
(+ x 2)
(* x 2)) => 10
((lambda ()
(if #t 3)
(if #f 0 4)
(if #t 2))) => 2

The other answer is completely right, but your interface is not very scheme-y. This is a more common form with tail recursion.
; Assumes given a list of numbers. Returns sum of even indices.
(define (mylength alist)
(let helper ((alist alist) (acc 0))
(cond
((null? alist) acc)
((null? (cdr alist)) (+ acc (car alist)))
(else (helper (cddr alist) (+ acc (car alist)))))))

Related

The object () is not applicable

I am writing a very simple program for Pascal's triangle in scheme (exercise from SICP). I wrote this code and its getting an error message The object () is not applicable.
(define (pascal-triangle n)
(define (get-value list position)
(define (get-sum-iter prevList counter)(
cond
((= counter (- position 1))
(+ (car prevList) (car (cdr prevList))))
(else (get-sum-iter (cdr prevList) (+ counter 1)))
))
(get-sum-iter list 0)
)
(define (create-list prevList size)
(define (create-list-iter currList counter)(
cond
((or (= counter (- size 1))
(= counter 0))
(create-list-iter (cons 1 currList) (- counter 1)))
((> counter 0)
create-list-iter
(cons (get-value prevList counter)
currList)
(- counter 1))
(else currList)
))
(create-list-iter () (- size 1))
)
(define (pascal-triangle-iter resultList counter)
(cond
((> counter n) resultList)
(else
(pascal-triangle-iter
(cons create-list
((cond
((null? resultList) resultList)
(else (car resultList)))
counter)
resultList)
(+ counter 1)))
)
)
(pascal-triangle-iter () 1)
)
In my function definition of pascal-triangle-iter I expect the first parameter to be a list of list. but when I'm calling it I am just passing an empty list and not list of list. My guess is that this is the cause of the error The object () is not applicable But when how do I initialize and empty list of list?
Thanks to everyone specially #barmar and #adabsurdum to helping me. The problem was the my parenthesis were off. The list construction was fine. The following code works fine for me (MIT Scheme on Mac OS)
; pascal triangle recursive
(define (pascal-triangle n)
(define (get-value list position)
(define (get-sum-iter prevList counter)(
cond
((= counter (- position 1)) (+ (car prevList) (car (cdr prevList))))
(else (get-sum-iter (cdr prevList) (+ counter 1)))
))
(get-sum-iter list 0)
)
(define (create-list prevList size)
(define (create-list-iter currList counter)(
cond
((or (= counter (- size 1)) (= counter 0)) (create-list-iter (cons 1 currList) (- counter 1)))
((> counter 0) (create-list-iter (cons (get-value prevList counter) currList) (- counter 1)))
(else currList)
))
(create-list-iter '() (- size 1))
)
(define (emptyOrFirst list)(
cond
((null? list) '())
(else (car list))
))
(define (pascal-triangle-iter resultList counter)
(cond
((> counter n) resultList)
(else (pascal-triangle-iter (cons (create-list (emptyOrFirst resultList) counter) resultList) (+ counter 1)))
)
)
(pascal-triangle-iter '() 1)
)

Functional version of deleting nth element in a list in Racket

I want to get a list which has nth version deleted from the original list. I could manage following code which is imperative:
(define (list-removeN slist n)
(define outl '())
(for ((i (length slist)))
(when (not (= i n))
(set! outl (cons (list-ref slist i) outl))))
(reverse outl))
What can be the functional equivalent of this? I tried for/list, but I have to insert #f or at that position, removing which is not ideal because #f or may occur at other positions in list also.
You can do it recursively with an accumulator. Something like
#lang racket
(define (remove-nth lst n)
(let loop ([i 0] [lst lst])
(cond [(= i n) (rest lst)]
[else (cons (first lst) (loop (add1 i) (rest lst)))])))
(remove-nth (list 0 1 2 3 4 5) 3)
(remove-nth (list 0 1 2 3) 3)
(remove-nth (list 0 1 2) 0)
This produces
'(0 1 2 4 5)
'(0 1 2)
'(1 2)
You could do it with for/list but this version traverses the list twice because of the length call.
(define (remove-nth lst n)
(for/list ([i (length lst)]
[elem lst]
#:when (not (= i n)))
elem))
There's also split-at, but again this may not be as optimal as it creates two lists and appends them.
(define (remove-nth lst n)
(let-values ([(left right) (split-at lst n)])
(append left (rest right))))
A typical roll your own implementation that is recursive and uses O(n) time and O(n) space.
(define (remove-nth lst i)
(let aux ((lst lst) (i i))
(cond ((null? lst) '()) ;; what if (< (length lst) i)
((<= i 0) (cdr lst)) ;; what if (< i 0)
(else (cons (car lst)
(aux (cdr lst) (sub1 i)))))))
A interative version that uses append-reverse from srfi-1. O(n) time and O(1) space.
(define (remove-nth lst i)
(let aux ((lst lst) (i i) (acc '()))
(cond ((null? lst) (reverse acc)) ;; what if (< (length lst) i)
((<= i 0) (append-reverse acc (cdr lst))) ;; what if (< i 0)
(else (aux (cdr lst) (sub1 i) (cons (car lst) acc))))))

number of zeros only in the even lists

What I need to add to count the number of zeros only in the even lists?
For example,
(count-zeroes '((1 1 1) (1 0 0) (1 1 1) (1 0 0)))
4
it is for one list.
(define count-zeroes
(lambda (list)
(cond ((null? list) 0)
((= 0 (car list)) (+ 1 (count-zeroes (cdr list))))
(else (+ 0 (count-zeroes (cdr list))))
)
)
)
(define count-zeroes
(lambda (list)
(cond ((null? list) 0) ; a
((= 0 (car list)) (+ 1 (count-zeroes (cdr list)))) ; b
(else (+ 0 (count-zeroes (cdr list))))))) ; c
If list is initially a list of lists, then (null? list) in line a can be true (when you get to the end of the list), but the condition (= 0 (car list)) in the line b will never be true, since (car list) will always be another list, and 0 isn't a list.
A better way to break this down would probably be to first extract the even positioned sublists, then flatten them into a single list, and then count the zeros in those. That's not the most efficient way to do it (you'll create some intermediate storage), but you should probably implement something like that first, and then gradually optimize it afterward.
It's also worth noting that lists are typically indexed starting with position zero, so the second, fourth, etc., elements in the list are the ones with odd positions, not even positions. Here's the kind of abstraction that might help you in getting started with this kind of approach:
(define first car)
(define rest cdr)
(define list* cons)
(define (odds list)
(if (null? list) '()
(evens (cdr list))))
(define (evens list)
(if (null? list) '()
(list* (first list)
(odds (rest list)))))
(define sample '((0 0 1) (0 1 0) (0 1 1) (1 0 0) (1 0 1) (1 1 0) (1 1 1)))
;; (display (odds sample))
;; => ((0 1 0) (1 0 0) (1 1 0))
An interesting question. If you are operating on a lists of lists you need to add car-cdr recuresion.
(define (count-zeroes lst) ;;don't override core function names with a variable
(cond ((null? lst) 0)
((pair? (car lst))
(+ (count-zeroes (car lst))
(count-zeroes (cdr lst))))
((= 0 (car lst))
(+ 1 (count-zeroes (cdr lst))))
(else (count-zeroes (cdr lst))))))
No as to only evens you are no longer counting zeros, so a new function name is in order. You could make up a higher order function like this.
(define (count-zeros-of selector lst)
(count-zeroes (selector lst)))
And make a general selector
(define (take-every-Xnth-at-y x y lst)
(cond ((null? lst) '())
((= y 0) (cons (car lst)
(take-every x (- x 1) (cdr lst))))
(else (take-every x (- y 1) (cdr lst)))))
To put it all together
(define (count-zeroes-of-even lst)
(count-zeroes-of
(lambda (lst) ;;to bad we can't do partial application
(take-every-Xnth-at-y 2 1 lst))
lst)
Note each of these parts do their one thing and do it well.

Is it possible to reference while recurring to anonymous function in scheme ?

I try to write function which add some number to every element in the list and then makes sum of all this terms.
(define (num-to-sumtup num)
(lambda (tup)
(cond
((null? tup) 0)
(else (+ (car tup) num (num-to-sumtup ())) ;; here I need to get reference on the inner function
is it possible?
You don't need to make it a anonymous procedure. It may have a name in the scope of num-to-sumtup. Here are some examples of ways to do it. The simples way to do this would be to use the rec syntax which is defined in the SRFI-31.
#!r6rs
(import (rnrs base)
(srfi :31))
(define (num-to-sumtup num)
(rec (recur tup)
(cond
((null? tup) 0)
(else (+ (car tup) num (recur (cdr tup)))))))
((num-to-sumtup 10) '(1 2 3 4 5)) ; ==> 65
You can also do this with define:
(define (num-to-sumtup num)
(define (sum-list tup)
(cond
((null? tup) 0)
(else (+ (car tup) num (sum-list (cdr tup))))))
sum-list); we return the locally named procedure
Using higher order procedures and cut from SRFI-26.
#!r6rs
(import (rnrs base)
(rnrs lists) ; fold-left
(srfi :26)) ; cut
(define (num-to-sumtup num)
(lambda (tup)
(fold-left (cut + <> <> num) 0 tup)))
Yes, it is possible. num-to-sumtup Takes a number as argument, and returns a function, that takes a list. So you execute it to get the function, and then execute that function.
(define (num-to-sumtup num)
(lambda (tup)
(cond
((null? tup) 0)
(else
(+ (car tup) num ((num-to-sumtup num) (cdr tup)))))))
;Value: num-to-sumtup
((num-to-sumtup 10) '(1 2 3 4 5))
;Value: 65
In the spirit of sylwester's answer here's another option
(define (num-to-sup num)
(lambda (tup)
(foldl (lambda (x y) (+ num x y)) 0 tup)))

Counting elements of a list and sublists

I'm trying to create a function to count all the elements in a list, including the elements of its sublists. initially, to get started, i came up with a basic function myList:
(define myLength
(lambda (L)
(cond
((null? L) 0)
(else (+ 1 (myLength (cdr L)))))))
However, it doesn't help me account for function calls like:
(numAtoms '()) "...should be 0"
(numAtoms '(())) "...should be 0"
(numAtoms '(1 1)) "...should be 2"
(numAtoms '(1 (1 1) 1)) "...should be 4"
(numAtoms '(1 (1 (1 1)) 1)) "...should be 5"
I'm trying to use basic functions like length, null?, and list?.
I think the trick here is to imagine how you can transform your input into the code that you'd want to use to compute the sum. Let's write each of your inputs in the fully expanded form, in terms of cons and '() and whatever other atoms appear in your data:
'() == '()
'(()) == (cons '() '())
'(1 1) == (cons 1 (cons 1 '()))
'(1 (1 1) 1) == (cons 1 (cons 1 (cons 1 '())) (cons 1 '()))
'(1 (1 (1 1)) 1) == ...
Now, look what would happen if you replaced each occurrence of cons with +, and each occurrence of '() with 0, and each occurrence of something that's not '() with 1. You'd have:
'() => 0 == 0
(cons '() '()) => (+ 0 0) == 0
(cons 1 (cons 1 '())) => (+ 1 (+ 1 0)) == 2
(cons 1 (cons 1 (cons 1 '())) (cons 1 '())) => (+ 1 (+ 1 (+ 1 0)) (+ 1 0)) == 4
... => ... == ...
Notice that those sums are exactly the values that you want! Based on this, it seems like you might not want to treat your input as a list so much as a tree built from cons cells. In general, you can map over a tree by specifying a function to apply to the recursive results of processing a pair, and a function to process the atoms of the tree:
(define (treeduce pair-fn atom-fn tree)
(if (pair? tree)
(pair-fn (treeduce pair-fn atom-fn (car tree))
(treeduce pair-fn atom-fn (cdr tree)))
(atom-fn tree)))
You could then implement that mapping of cons to + and everything else to 1 if it's a list and 0 if it's not by:
(define (non-null-atoms tree)
(treeduce +
(lambda (atom)
(if (not (null? atom))
1
0))
tree))
This yields the kinds of results you'd expect:
(non-null-atoms '()) ;=> 0
(non-null-atoms '(())) ;=> 0
(non-null-atoms '(1 1)) ;=> 2
(non-null-atoms '(1 (1 1) 1)) ;=> 4
(non-null-atoms '(1 (1 (1 1)) 1)) ;=> 5
Here is a recursive template you can use:
(define (num-atoms lst)
(cond ((pair? lst) (+ (num-atoms <??>)
(num-atoms <??>)))
((null? lst) <??>) ; not an atom
(else <??>))) ; an atom
This next example uses a helper that has the accumulated value (num) as an argument.
(define (num-atoms lst)
;; locally defined helper
(define (helper num lst)
(cond ((pair? lst) (helper (helper <??> <??>) <??>)) ; recurse with the sum of elements from car
((null? lst) <??>) ; return accumulated value
(else (helper <??> <??>)))) ; recurse with add1 to num
;; procedure starts here
(helper 0 lst))
Hope it helps
Make my-length work for any argument type, list or 'atom'; then the recursive algorithm becomes almost trivial:
(define (my-length l)
(cond ((null? l) 0)
((list? l) (+ (my-length (car l)) (my-length (cdr l))))
(else 1))) ; atom
> (my-length '(1 (1 (1 1)) 1)))
5

Resources