How to implement symbol-length procedure in scheme - functional-programming

I am new to functional programming and trying to implement a procedure that returns the length of a symbol. Here is what i think: I give one parameter to it named "inSym" to return its length.
(define symbol-length (lambda (inSym) ( ...) )
But, i do not know how do i iterate over the inSym to find the number of characters in it. Can anyone give some help? Note that i do not want to use any built in functions or convert symbol into a string.
Thank you

You don't iterate over the symbol. Instead, convert the symbol to a string (symbol->string) and get its length (string-length).

Related

String Comparison in Common Lisp

I am new to Common Lisp and Functional programming in general. I have a function lets call it "wordToNumber", I want it to check if the input string is "one" "two" "three".. etc (0-9) only. and I want to return 1 2 3 etc. so (wordToNumber "one") should output the number 1. I'm having some trouble with string comparison, tried using eq and eql, but its not working, from what I read it is comparing memory location not actual string. Is there an easier way to go about this or is there someway to compare strings. I need any examples to be purely functional programming, no loops and stuff. This is a small portion of a project I'm working on for school.
Oh, for string comparison im just using a simple function at the moment like this:
(defun wordToNumber(x)
(if(eq 'x "one")(return-from wordToNumber 1)))
and calling it with this : (wordToNumber "one")
keep getting Nil returned
Thanks for any help
The functions to compare strings are string= and string-equal, depending on whether you want the comparison to be case-sensitive.
And when you want to compare the value of a variable, you mustn't quote it, since the purpose of quoting is to prevent evaluation.
(defun word-to-number (x)
(cond ((string-equal x "one") 1)
((string-equal x "two") 2)
((string-equal x "three") 3)
...
))
As a practical matter, before you make a ten-branch conditional, consider this: you can pass string= and string-equal (and any other binary function) as an :test argument to most of the sequence functions. Look though the sequence functions and see if there's something that seems relevant to this problem. http://l1sp.org/cl/17.3 (There totally is!)
One nice thing about Lisp is the apropos function. Lisp is a big language and usually has what you want, and (apropos "string") would prolly have worked for you. I recommend also the Lisp Hyperpec: http://www.lispworks.com/documentation/HyperSpec/Front/
eq is good for symbols, CLOS objects, and even cons cells but be careful: (eq (list 1) (list 1)) is false because each list form returns a different cons pointing to the same number.
eql is fine for numbers and characters and anything eq can handle. One nice thing is that (eql x 42) works even if x is not a number, in which case (= x 42) would not go well.
You need equal for lists and arrays, and strings are arrays so you could use that. Then there is equalp, which I will leave as an exercise.

If a string in lisp is a vector, why can't I access the first element using svref?

So, I'm trying to learn Lisp, and I've come across a problem in the definition of what a String is.
I'm reading the ANSI Common Lisp by Paul Graham, and in this book it states that a String is a vector, or one-dimensional Array.
So, I create a String:
(defvar *my-string* "abc")
And then I can access the first value of my-string this way:
(aref *my-string* 0)
But if it is a vector, why can't I access that element this way:
(svref *my-string* 0)
I mean, when I create a vector this way:
(defvar my-vec (make-array 4 :initial-element 1))
I can access first element using svref:
(svref my-vec 0) ; returns 1
I forgot to add error when I try svref on String:
"The value "abc" is not of type (SIMPLE-ARRAY T (*))."
String is a vector, but it isn't a simple-vector. svref takes a simple-vector as first argument.
You can check it by calling:
(vector-p *my-string*)
which returns true
Unlike:
(simple-vector-p *my-string*)
which returns false.
Notice that (simple-vector-p my-vec) will return true as well, which confirms that make-array creates a simple-vector.
soulcheck's answer is absolutely right, but it's worth the time to become comfortable with the HyperSpec. For instance, if you start at the page for svref, there's a note at the bottom:
Notes:
svref is identical to aref except that it requires its first argument to be a simple vector.
The glossary entry for simple vector (linked above) says:
simple vector n. a vector of type simple-vector, sometimes called a "simple general vector." Not all vectors that are simple are simple vectors—only those that have element type t.
15.2 The Arrays Dictionary is also helpful here, as is 15. Arrays as a whole.

Generating a bitvector in scheme

I am trying to implement a spellchecker that takes a hash function and a dictionary, and then map the hash values of the words to a bitvector. More specifically, I am trying to write a function called gen-checker that takes as input a list of hash functions and a dictionary of words and returns a spellchecker. The spellchecker must generate a bitvector representation for the input of the dictionary, which contains #t or #f indicating the correct or incorrect spelling of the word.
I have already defined the has functions and have a dictionary to use, but I can't seem to get the bit vector setup
I have tried implementing (make-bitvector 8 #f) found here:
http://www.gnu.org/software/guile/manual/html_node/Bit-Vectors.html
But for some reason drracket does not recognize it. What am I doing wrong? How to implement the bitvector representation?
It may seem like this answer is joking, but it is not:
(define make-bitvector make-vector)
(define bitvector-ref vector-ref)
;; ...
After everything is working, and only then, would one need to optimize storage by bit packing.

Is it possible in Common Lisp to disable return values for specified functions?

I would like to know whether or not it's possible to disable return values for specified functions. I am using compiler SBCL. I am asking this, because it takes a while to print the return value, and I don't even need it. Any ideas?
OK, it does the job:
(progn
(...)
t)
Also, thanks for *print-length*.
I find it useful to limit the amount of data printed to my reply by setting the *print-length* variable to a relatively low value in my lisp startup file, like so:
(setf *print-length* 20)
That way, I do not have to worry too much about functions that return a large number of elements.
Using the function values it is possible to return zero (or several) values from a function.
values returns all it's arguments. Thus a function having (values) as it's last form will not return anything, while a function ending with (values val1 val2 val3) will return three values. When calling a function returning several values, only the first one (the primary return value) is available in the normal way, while the other ones may be captured using e.g. multiple-value-bind See the section on Return Values in the Hyperspec for further details
If you want to limit the output from a function that you can not modify, you can call it like this:
(progn
(function-returning-much-data)
(values))

Creating own substring functions recursively in Ocaml

How can i write a substring function in ocaml without using any assignments lists and iterations, only recursions? i can only use string.length.
i tried so far is
let substring s s2 start stop=
if(start < stop) then
substring s s2 (start+1) stop
else s2;;
but obviously it is wrong, problem is that how can i pass the string that is being built gradually with recursive calls?
This feels like a homework problem that is intended to teach you think think about recursion. For me it would be easier to think about the recursion part if you decide on the basic operations you're going to use. You can't use assignments, lists, or iterations, okay. You need to extract parts of your input string somehow, but you obviously can't use the built-in substring function to do this, that would defeat the purpose of the exercise. The only other operation I can think of is the one that extracts a single character from a string:
# "abcd".[2];;
- : char = 'c'
You also need a way to add a character to a string, giving a longer string. But you're not allowed to use assignment to do this. It seems to me you're going to have to use String.make to translate your character to a string:
# String.make 1 'a';;
- : string = "a"
Then you can concatenate two strings using the ^ operator:
# "abc" ^ "def"
- : string = "abcdef"
Are you allowed to use these three operations? If so, you can start thinking about the recursion part of the substring problem. If not, then I probably don't understand the problem well enough yet to give advice. (Or maybe whoever set up the restrictions didn't expect you to have to calculate substrings? Usually the restrictions are also a kind of hint as to how you should proceed.)
Moving on to your specific question. In beginning FP programming, you don't generally want to pass the answer down to recursive calls. You want to pass a smaller problem down to the recursive call, and get the answer back from it. For the substring problem, an example of a smaller problem is to ask for the substring that starts one character further along in the containing string, and that is one character shorter.
(Later on, you might want to pass partial answers down to your recursive calls in order to get tail-recursive behavior. I say don't worry about it for now.)
Now I can't give you the answer to this, Partly because it's your homework, and partly because it's been 3 years since I've touched OCaml syntax, but I could try to help you along.
Now the Basic principle behind recursion is to break a problem down into smaller versions of itself.
You don't pass the string that is slowly being built up, instead use your recursive function to generate a string that is almost built up except for a single character, and then you add that character to the end of the string.

Resources