Given that myVector is a vector and i is an index , when I do this :
(= (vector-ref myVector i) #\.)
I get :
=: contract violation
expected: number?
given: #\.
argument position: 1st
other arguments...:
#\.
How can I check if the character at location i is a . ?
You should not convert the character to a number, simply use the char=? procedure, from the documentation:
Returns #t if all of the arguments are eqv?
For your example in particular:
(define myVector #(#\1 #\. #\2))
(define i 1)
(char=? (vector-ref myVector i) #\.)
=> #t
Several of your questions of the last couple of days have been related to character or string manipulation, I'd advise you take a good look at the documentation page dealing with those subjects.
Edit: This incorrectly assumed the vector was of number and not characters. Use Oscar's answer.
You need to convert the number to a character or viceversa.
Eg:
(= (vector-ref myVector i) (char->integer #\.))
Or
(char=? (integer->char (vector-ref myVector i)) #\.)
Related
I am trying to understand how the count-even function given in my textbook works and I'm not sure if I completely understand it.
I have provided my understanding of the function in comments next to code. Can someone explain what is happening and correct me in each step of this function.
And I don't understand how this function is recursive. What is the base case ?
(define (ce x) ; x is an argument that takes in a list
(if (pair? x) ; I don't understand why this is here
(+ (ce (car x)) ; Add the resuls of recursive call together (0 or 1)
(ce (cdr x)))
(if (and (number? x) (even? x)) ; if x is a number and x is even then return 1 ? Else return 0 ?
1
0) ) )
In a recursive function there are usually two cases:
The result is a call to the same function
The result is a simple value, the base case
You choose between the two cases with an if.
Hence the base case is:
(if (and (number? x) (even? x))
1
0) ) )
And the recursive case is:
(+ (count-evens (car x))
(count-evens (cdr x)))
Comments:
The argument x doesn't need to be a list. pairs? tests for a list. If it is just a value then we have the base case. If an even number then result is one, else zero.
If the argument x is a list, we split it into two parts, the head (car) and the tail (cdr). The head is just a value, and so when we rerun count-evens on it we end up with the base case.
The tail is passed to count-evens which keeps slicing off a value at a time until we have a value in the car and empty list in cdr. In the base case, the first value is assessed for an even number, and the empty list is always evaluated as zero.
I am writing a program that recursively iterates through a list, provided the index of the current character and a list of characters. However, when I run the following program:
(defun printAllElementsRecursively (index providedList)
(if (>= index (length providedList))
(return-from printAllElementsRecursively NIL)
)
(defvar currCharacter (nth index providedList))
(print (format nil "Character at index ~a: ~a" index currCharacter))
(printAllElementsRecursively (+ index 1) providedList)
)
(printAllElementsRecursively 0 '(A B B A))
I get the following output:
"Character at index 0: A"
"Character at index 1: A"
"Character at index 2: A"
"Character at index 3: A"
This seems strange, considering that the value of index does increment correctly.
You are misusing defvar:
It should never be used inside a function, use let instead or just (nth index providedList) instead of currCharacter.
It defines a new global variable, and only sets it if it has not been set yet, so it sets
currCharacter once only.
You also do not really need return-from, and your code
would be more readable if use used dashes instead of camel case.
E.g.,
(defun print-list-elements-recursively (list)
(when list
(print (first list))
(print-list-elements-recursively (rest list))))
Also, nth is linear in its list argument's length,
so your function is quadratic in it (my version is linear).
I am trying to create a recursive function which picks n items from a list returning the picked values and the list without the values, but when I create my variables I get this error:
new-list: unbound identifier in: new-list
Here is my code:
(define(pick-randomr list n picked) ;;Picked always called as empty list
(if(= n 0) (values list picked)
((let* ([aux list]
[r (random (length aux))]
[value (list-ref aux r)]
[new-picked (cons value picked)]
[new-list (values (remove value aux))])
(values aux r new-list))
(pick-randomr new-list (- n 1) new-picked))))
EDIT:
The line that goes:
(values aux r new-list)
is just to not have an empty body
There are a couple of problems with your syntax:
You should not use list as a parameter name, it conflicts with a built-in procedure of the same name.
Don't surround let* with two brackets, that's a common mistake, brackets are not like curly braces in other languages, you must not use them to define a block of statements, use begin for that - but we don't need it in this particular case.
The first error you got stated that you must not define a let* with an empty body. But the expression you added there isn't right, you must write the expressions that use the variables inside the let*, otherwise the new-list variable won't be visible.
This is what you meant to write:
(define (pick-randomr lst n picked)
(if (= n 0)
(values lst picked)
(let* ([aux lst]
[r (random (length aux))]
[value (list-ref aux r)]
[new-picked (cons value picked)]
[new-list (values (remove value aux))])
(pick-randomr new-list (- n 1) new-picked))))
Let's test it:
(pick-randomr '(1 2 3 4 5) 2 '())
=> '(1 2 5)
'(3 4)
i'm trying to write a code in RACKET , i know how to solve it but i'm having some trouble , i can use your help .
the function will get a list and a specific symbol , and it need to return the number of times that the symbol is shown in the list .
in the test - i'm comparing the result with a number i'm asking and should return true if the number is the same .
i've tried to do it with (if / cond / and even tried acc ) - but there is always something that is missing .
here is my code including the test .
please help me find out how to write it .
the idea of the solution is , the i take the head of the list and i check if it's equal to the symbol i wrote , if it does - n is plus 1 , empty list is equal 0 .
( : counts : (Listof Symbol) -> Integer )
(define (counts a n ) ; a = list of symbols.
(cond [(null? a) 0])
[(eq?(first a) 'x) (= n(+ n 1))]
(counts( (rest a) n)))
;test:
(test (eq? (counts ('a 'b 'x) )1))
There are several problems with your code:
The cond expression is being incorrectly used, and the else case is missing
There are erroneous parentheses, for example at the end of the second line in counts and when you call counts in the fourth line
In the base case of the recursion you must return n, the counter
You must also call the recursion if the symbol was found, in the second case
This part: (= n (+ n 1)) is not doing what you think, it's not changing the value of n, instead is testing for equality between n and (+ n 1) (which will return false, of course)
You're not passing as parameter the symbol being searched, it's hard-coded in the procedure
This is a corrected version of what you intended to write:
(define (counts a x n)
(cond [(null? a) n]
[(eq? (first a) x)
(counts (rest a) x (+ n 1))]
[else
(counts (rest a) x n)]))
Use it like this:
(counts '(a b x c d x e x) 'x 0)
=> 3
I advice you to grab a good book or tutorial, it seems you're struggling with the basic syntax. And learn how to use DrRacket to help with the syntax errors, it's an excellent IDE.
I wrote a program below for "Define a procedure leet-speak takes a string and returns the result of changing all s's to fives, all e's to threes, all l's to ones, and all o's to zeros. Do not write any recursive code to do this. Simply make use of string->list, map, and list->string."
The error I got is:
~ (leet-speak "leet speak neat speak")
Exception: attempt to apply non-procedure (1 3 3 #\t #\space 5 ...)
Here is my definition for leet-speak:
(define leet-speak
(lambda (y)
(list->string
((map
(lambda (x)
(cond
[(eq? #\l x) 1]
[(eq? #\s x) 5]
[(eq? #\o x) 0]
[(eq? #\e x) 3]
[else x])
) (string->list y )))))
I really can't find out where the problem is.
You have too many parentheses around the map. Remove the extra so that there's only one parenthesis before map, and you should be good to go.
Your cond also needs to return the character corresponding to the number, not the number itself. Also, consider using a case instead of the cond you have.
All up, here's how it would look:
(define (leet-speak str)
(list->string
(map (lambda (x)
(case x
[(#\l) #\1]
[(#\s) #\5]
[(#\o) #\0]
[(#\e) #\3]
[else x]))
(string->list str))))