Strange comparison of `'quote` and `'lambda` in Scheme (guile) - switch-statement

Let see my code
Code1:
(eqv? 'lambda 'quote)
return #f
Code2:
(case 'lambda ('quote "equal") (else "not equal"))
return "not equal" but generate a warning ;;; <stdin>:17:0: warning: duplicate datum quote in clause ((quote quote) "equal") of case expression (case (quote lambda) ((quote quote) "equal") (else "not equal"))
Code3: strange result
(case 'quote ('lambda "equal"))
return "equal" and without warning
I interpreted code using guile (GNU Guile) 2.0.11. And here is the description of case syntax from gnu
The the result of this evaluation is compared against all datums using eqv?

'<something> is an abbreviation for the list (quote <something>). In Scheme, the case form should be a list (in some Lisps, e.g., Common Lisp, it can also be a single non-list element, representing the list of just that element), so that you can do, e.g.:
(case n
((0) 'zero) ; in Common Lisp, could also be (0 'zero)
((1 2 3) 'one-two-or-three)
((4 5) 'four-or-five))
When you do:
(case some-symbol
('quote 'it-was-quote))
You're doing
(case some-symbol
((quote quote) 'it-was-quote))
So you're providing a list, and it's got a duplicate element. There's nothing wrong with that, but it's usually unexpected, and so you get a warning.
By that reasoning
(case 'quote ('lambda "equal"))
is the same as
(case 'quote ((quote lambda) "equal"))
and, since the symbol quote is in the list (quote lambda), the case matches, and you get "equal". The moral of the story is: "don't quote the cases in case." That is, you should be doing:
(case 'quote
((lambda) "equal"))

Related

string-ref function doesn't work as should

In my code i am trying to check if a string have a specific prefix,
to do so,I am using the equal? and string-ref function.
but it doesn't seem to work as expected
here is the part I'm talking about:
(: plPrefixContained : String -> Boolean)
(define (plPrefixContained x )
(equal? (string-ref x 0) "p"))
(test (plPrefixContained "pcenuc") => true)
I was checking this specific function, that should return true, but I keep getting false for the test.
I was trying to change "p" to #/p" and I was trying to use string=? and eq? insted of equal? but nothing.
Any help would be appreciated
(test (plPrefixContained "pcenuc") => true)
I'm using DRracket and the language is #lang pl
Remember that string-ref returns a character, not a string. For the comparison to succeed, use #\p instead of "p".
(define (plPrefixContained x )
(equal? (string-ref x 0) #\p))
The above will work. But to make it more explicit, you should use char=? instead of equal?, in this way you'll remember that the comparison is between characters.

Switch statement in Lisp

Switch statement with Strings in Lisp.
(defun switch(value)
(case value
(("XY") (print "XY"))
(("AB") (print "AB"))
)
)
I want to compare if value is "XY" then print "XY" or same for "AB".
I have tried this code but it gives me nil. Can some please tell me what i am doing wrong?
You can use the library alexandria, which has a configurable switch macro:
(switch ("XY" :test 'equal)
("XY" "an X and a Y")
("AB" "an A and a B"))
print("XY") looks more like Algol (and all of its descendants) rather than LISP. To apply print one would surround the operator and arguments in parentheses like (print "XY")
case happens to be a macro and you can test the result yourself with passing the quoted code to macroexpand and in my implementation I get:
(let ((value value))
(cond ((eql value '"XY") (print "XY"))
((eql value '"AB") (print "AB"))))
You should know that eql is only good for primiitive data types and numbers. Strings are sequences and thus (eql "XY" "XY") ;==> nil
Perhaps you should use something else than case. eg. use cond or if with equal.
The Hyperspec on CASE says:
These macros allow the conditional execution of a body of forms in a clause that is selected by matching the test-key on the basis of its identity.
And strings are not identical in CL, i.e. (EQ "AB" "AB") => NIL.
That is why CASE wouldn't work for strings. You either need to use symbols (they are interned once only, thus guaranteeing identity) or use COND with EQUAL or even EQUALP if the letters case to be ignored.

String Switch in Common Lisp

How do I do a conditional dispatch on a string in Common Lisp? I'm looking for something like:
(case str
("a" "found an a")
("abc" "found an abc")
(otherwise "other"))
so (case "a") returns "found an a"
Alexandria has the macros switch, cswitch, eswitch:
(switch (str :test #'equal)
("a" "found an a")
("abc" "found an abc")
(t "other"))
The string-case library implements this functionality. It is available from quicklisp.
A trivial (and potentially slow) variant would be something like:
(defmacro string-case (str &rest forms)
(let* ((strval (gensym "STRVAL"))
(cond-body (loop for (s . f) in forms
collect `((string= ,strval ,s) ,#f))))
`(let ((,strval ,str)) (cond ,#cond-body))))
This (unfortunately) does not allow for an else, otherwise or grouping of strings, but making that extension should be pretty straight-forward. Using the existing string-case from quicklisp is probably the better choice.
This is from the Practical Common LISP by Seibel only changes made are using the read-char instead of read-line. This worked for me using simple characters "A" "B" etc. To see what the characters and strings actually are try using (format nil "~:C" c) c being character and (format nil "~:S" s) s being string.
(defun prompt-read(prompt)
(format *query-io* "~a: " prompt)
(force-output *query-io*)
(read-char *query-io*))
(defun move()
(case(prompt-read "ROW[A-C]")
(#\A (print "A"))
(#\B (print "B"))
(#\C (print "C"))))
I was able to get it working by converting the string to a symbol:
(case (intern (string-upcase str))
(a "found an a")
(abc "found an abc")
(otherwise "other"))

Split a string even if the last character is a delimiter

I want to delete some characters at the end of a string.
I made this function :
(defun del-delimiter-at-end (string)
(cond
((eq (delimiterp (char string (- (length string) 1))) nil)
string )
(t
(del-delimiterp-at-end (subseq string 0 (- (length string) 1))) ) ) )
with this :
(defun delimiterp (c) (position c " ,.;!?/"))
But I don't understand why it doesn't work. I have the following error :
Index must be positive and not -1
Note that I want to split a string in list of strings, I already looked here :
Lisp - Splitting Input into Separate Strings
but it doesn't work if the end of the string is a delimiter, that's why I'm trying to do that.
What am I doing wrong?
Thanks in advance.
The Easy Way
Just use string-right-trim:
(string-right-trim " ,.;!?/" s)
Your Error
If you pass an empty string to you del-delimiter-at-end, you will be passing -1 as the second argument to char.
Your Code
There is no reason to do (eq (delimiterp ...) nil); just use (delimiterp ...) instead (and switch the clauses!)
It is mode idiomatic to use if and not cond when you have just two clauses and each has just one form.
You call subseq recursively, which means that you not only allocate memory for no reason, your algorithm is also quadratic in string length.
There are really two questions here. One is more specific, and is described in the body of the question. The other is more general, and is what the title asks about (how to split a sequence). I'll handle the immediate question that's in the body, of how to trim some elements from the end of a sequence. Then I'll handle the more general question of how to split a sequence in general, and how to split a list in the special case, since people who find this question based on its title may be interested in that.
Right-trimming a sequence
sds answered this perfectly if you're only concerned with strings. The language already includes string-right-trim, so that's probably the best way to solve this problem, if you're only concerned with strings.
A solution for sequences
That said, if you want a subseq based approach that works with arbitrary sequences, it makes sense to use the other sequence manipulation functions that the language provides. Many functions take a :from-end argument and have -if-not variants that can help. In this case, you can use position-if-not to find the rightmost non-delimiter in your sequence, and then use subseq:
(defun delimiterp (c)
(position c " ,.;!?/"))
(defun right-trim-if (sequence test)
(let ((pos (position-if-not test sequence :from-end t)))
(subseq sequence 0 (if (null pos) 0 (1+ pos)))))
(right-trim-if "hello!" 'delimiterp) ; some delimiters to trim
;=> "hello"
(right-trim-if "hi_there" 'delimiterp) ; nothing to trim, with other stuff
;=> "hi_there"
(right-trim-if "?" 'delimiterp) ; only delimiters
;=> ""
(right-trim-if "" 'delimiterp) ; nothing at all
;=> ""
Using complement and position
Some people may point out that position-if-not is deprecated. If you don't want to use it, you can use complement and position-if to achieve the same effect. (I haven't noticed an actual aversion to the -if-not functions though.) The HyperSpec entry on complement says:
In Common Lisp, functions with names like xxx-if-not are related
to functions with names like xxx-if in that
(xxx-if-not f . arguments) == (xxx-if (complement f) . arguments)
For example,
(find-if-not #'zerop '(0 0 3)) ==
(find-if (complement #'zerop) '(0 0 3)) => 3
Note that since the xxx-if-not functions and the :test-not
arguments have been deprecated, uses of xxx-if functions or :test
arguments with complement are preferred.
That said, position and position-if-not take function designators, which means that you can pass the symbol delimiterp to them, as we did in
(right-trim-if "hello!" 'delimiterp) ; some delimiters to trim
;=> "hello"
complement, though, doesn't want a function designator (i.e., a symbol or function), it actually wants a function object. So you can define right-trim-if as
(defun right-trim-if (sequence test)
(let ((pos (position-if (complement test) sequence :from-end t)))
(subseq sequence 0 (if (null pos) 0 (1+ pos)))))
but you'll have to call it with the function object, not the symbol:
(right-trim-if "hello!" #'delimiterp)
;=> "hello"
(right-trim-if "hello!" 'delimiterp)
; Error
Splitting a sequence
If you're not just trying to right-trim the sequence, then you can implement a split function without too much trouble. The idea is to increment a "start" pointer into the sequence. It first points to the beginning of the sequence. Then you find the first delimiter and grab the subsequence between them. Then find the the next non-delimiter after that, and treat that as the new start point.
(defun split (sequence test)
(do ((start 0)
(results '()))
((null start) (nreverse results))
(let ((p (position-if test sequence :start start)))
(push (subseq sequence start p) results)
(setf start (if (null p)
nil
(position-if-not test sequence :start p))))))
This works on multiple kinds of sequences, and you don't end up with non delimiters in your subsequences:
CL-USER> (split '(1 2 4 5 7) 'evenp)
((1) (5 7))
CL-USER> (split '(1 2 4 5 7) 'oddp)
(NIL (2 4))
CL-USER> (split "abc123def456" 'alpha-char-p)
("" "123" "456")
CL-USER> (split #(1 2 3 foo 4 5 6 let 7 8 list) 'symbolp)
(#(1 2 3) #(4 5 6) #(7 8))
Although this works for sequences of all types, it's not very efficient for lists, since subseq, position, etc., all have to traverse the list up to the start position. For lists, it's better to use a list specific implementation:
(defun split-list (list test)
(do ((results '()))
((endp list)
(nreverse results))
(let* ((tail (member-if test list))
(head (ldiff list tail)))
(push head results)
(setf list (member-if-not test tail)))))
CL-USER> (split-list '(1 2 4 5 7) 'oddp)
(NIL (2 4))
CL-USER> (split-list '(1 2 4 5 7) 'evenp)
((1) (5 7))
Instead of member-if and ldiff, you could also us cut from this answer to Idiomatic way to group a sorted list of integers?.

check if string are just letters (no symbols and numbers) lisp

i need to check if a word are just with letter
example "hfjel" return true
"df(f0" return false
and anyone can explain me the function symbol-name
thank you for help
There is a handy standard function called alpha-char-p that does what you're asking for.
CL-USER(1): (alpha-char-p #\a)
T
CL-USER(2): (alpha-char-p #\Γ)
T
CL-USER(3): (alpha-char-p #\α)
T
CL-USER(4): (alpha-char-p #\0)
NIL
CL-USER(5): (alpha-char-p #\.)
NIL
You can use it in conjunction with every:
CL-USER(7): (every #'alpha-char-p "word")
T
CL-USER(8): (every #'alpha-char-p "nonword7")
NIL
CL-USER(9): (every #'alpha-char-p "non-alpha-word")
NIL
CL-USER(10): (every #'alpha-char-p "今日は")
T
OK, I commented above on diacritics because this particular case often goes unnoticed, below is an example:
* (defparameter *weird-letter*
(coerce (list (code-char #x0438)
(code-char #x0306)) 'string))
*WEIRD-LETTER*
* *weird-letter*
"и"
* (length *weird-letter*)
2
* (every #'alpha-char-p *weird-letter*)
NIL
I'm actually not sure what different Lisp implementations will do here because Unicode support is different in some of them (so far I can tell).
For the code above, the expected result must've been T, because, in fact, two codepoints U+0438-U+0306 constitute a single letter with diacritic. There are two ways in Unicode to spell it, one is a single character, and another one is a combination of the same letter without the diacritic and the diacritic.
So, if you wanted to be super-correct, then you would have to check if, by chance, the letter is not followed by diacritic, but (lo and behold!) only some of these are actually valid letters! Unicode is serious business...
EDIT:
In order to better illustrate my case:
#!/opt/ActivePerl-5.14/bin/perl
binmode STDOUT, ":utf8";
my $weird_letter = "\x{0438}\x{0306}";
print "$weird_letter\n";
if ($weird_letter =~ m/^(\pL|(\pL\pM))+$/)
{ print "it is a letter!\n"; }
else { print "it is not a letter!\n"; }
The above would almost fairly treat this kind of characters.

Resources