Displaying a string while using cond in Lisp - string

I'm just starting off with Lisp and need some help. This is technically homework, but I gave it a try and am getting somewhat what I wanted:
(defun speed (kmp)
(cond ((> kmp 100) "Fast")
((< kmp 40) "Slow")
(t "Average")))
However, if I run the program it displays "Average" instead of just Average (without the quotes).
How can I get it to display the string without quotes?

You can use symbols instead of strings. But keep in mind that symbols will be converted to uppercase:
> 'Average
AVERAGE
If you care about case or want to embed spaces, use format:
> (format t "Average")
Average

The read-eval-print loop displays the return value of your function, which is one of the strings in a cond branch. Strings are printed readably by surrounding them with double-quotes.
You could use (write-string (speed 42)). Don't worry that it also shows the string in double-quotes - that's the return value of write-string, displayed after the quoteless output.

You can also use symbols instead of strings:
(defun speed (kmp)
(cond ((> kmp 100) 'fast)
((< kmp 40) 'slow)
(t 'average)))
Symbols are uppercased by default, so internally fast is then FAST.
You can write any symbol in any case and with any characters using escaping with vertical bars:
|The speeed is very fast!|
Above is a valid symbol in Common Lisp and is stored internally just as you write it with case preserved.

Related

General method to trim non-printable characters in Clojure

I encountered a bug where I couldn't match two seemingly 'identical' strings together. For example, the following two strings fail to match:
"sample" and "​sample".
To replicate the issue, one can run the following in Clojure.
(= "sample" "​sample") ; returns false
After an hour of frustrated debugging, I discovered that there was a zero-width space at the front of the second string! Removing it from this particular example via a backspace is trivial. However I have a database of strings that I'm matching, and it seems like there are multiple strings facing this issue. My question is: is there a general method to trim zero-width spaces in Clojure?
Some method's I've tried:
(count (clojure.string/trim "​abc")) ; returns 4
(count (clojure.string/replace "​abc" #"\s" "")) ; returns 4
This thread Remove zero-width space characters from a JavaScript string does provide a solution with regular expressions that works in this example, i.e.
(count (clojure.string/replace "​abc" #"[\u200B-\u200D\uFEFF]" "")) ; returns 3
However, as stated in the post itself, there are many other potential ascii characters that may be invisible. So I'm still interested if there's a more general method that doesn't rely on listing all possible invisible unicode symbols.
I believe, what you are referring to are so-called non-printable characters. Based on this answer in Java, you could pass the #"\p{C}" regular expression as pattern to replace:
(defn remove-non-printable-characters [x]
(clojure.string/replace x #"\p{C}" ""))
However, this will remove line breaks, e.g. \n. So in order to keep those characters, we need a more complex regular expression:
(defn remove-non-printable-characters [x]
(clojure.string/replace x #"[\p{C}&&^(\S)]" ""))
This function will remove non-printable characters. Let's test it:
(= "sample" "​sample")
;; => false
(= (remove-non-printable-characters "sample")
(remove-non-printable-characters "​sample"))
;; => true
(remove-non-printable-characters "sam\nple")
;; => "sam\nple"
The \p{C} pattern is discussed here.
The regex solution from #Rulle is very nice. The tupelo.chars namespace also has a collection of character classes and predicate functions that could be useful. They work in Clojure and ClojureScript, and also include the ^nbsp; for browsers. In particular, check out the visible? predicate.
The tupelo.string namespace also has a number of helper & convenience functions for string processing.
(ns tst.demo.core
(:use tupelo.core tupelo.test)
(:require
[tupelo.chars :as chars]
[tupelo.string :as str] ))
(def sss
"Some multi-line
string." )
(dotest
(println "result:")
(println
(str/join
(filterv
#(or (chars/visible? %)
(chars/whitespace? %))
sss))))
with result
result:
Some multi-line
string.
To use, make your project.clj look like:
:dependencies [
[org.clojure/clojure "1.10.2-alpha1"]
[prismatic/schema "1.1.12"]
[tupelo "20.07.01"]
]

In DrRacket how do I check if a string has a certain amount of characters, as well how do I determine what the first character in a string is

Basically I have a problem, here is the information needed to solve the problem.
PigLatin. Pig Latin is a way of rearranging letters in English words for fun. For example, the sentence “pig latin is stupid” becomes “igpay atinlay isway upidstay”.
Vowels(‘a’,‘e’,‘i’,‘o’,and‘u’)are treated separately from the consonants(any letter that isn’t a vowel).
For simplicity, we will consider ‘y’ to always be a consonant. Although various forms of Pig Latin exist, we will use the following rules:
(1) Words of two letters or less simply have “way” added on the end. So “a” becomes “away”.
(2) In any word that starts with consonants, the consonants are moved to the end, and “ay” is added. If a word begins with more than two consonants, move only the first two letters. So “hello” becomes “ellohay”, and “string” becomes “ringstay”.
(3) Any word which begins with a vowel simply has “way” added on the end. So “explain” becomes “explainway”.
Write a function (pig-latin L) that consumes a non-empty (listof Str) and returns a Str containing the words in L converted to Pig Latin.
Each value in L should contain only lower case letters and have a length of at least 1.
I understand that i need to set three main conditions here, i'm struggling with Racket and learning the proper syntax to write out my solutions. first I need to make a conditions that looks at a string and see if it's length is 2 or less to meet the (1) condition. For (2) I need to look at the first two characters in a string, i'm assuming I have to convert the string into a list of char(string->list). For (3) I understand I just have to look at the first character in the string, i basically have to repeat what I did with (2) but just look at the first character.
I don't know how to manipulate a list of char though. I also don't know how to make sure string-length meets a criteria. Any assistance would be appreciated. I basically have barely any code for my problem since I am baffled on what to do here.
An example of the problem is
(pig-latin (list "this" "is" "a" "crazy" "exercise")) =>
"isthay isway away azycray exerciseway"
The best strategy to solve this problem is:
Check in the documentation all the available string procedures. We don't need to transform the input string to a list of chars to operate upon it, and you'll find that there are existing procedures that meet all of our needs.
Write helper procedures. In fact, we only need a procedure that tells us if a string contains a vowel at a given position; the problem states that only a-z characters are used so we can negate this procedure to also find consonants.
It's also important to identify the best order to write the conditions, for example: conditions 1 and 3 can be combined in a single case. This is my proposal:
(define (vowel-at-index? text index)
(member (string-ref text index)
'(#\a #\e #\i #\o #\u)))
(define (pigify text)
; cases 1 and 3
(cond ((or (<= (string-length text) 2)
(vowel-at-index? text 0))
(string-append text "way"))
; case 2.1
((and (not (vowel-at-index? text 0))
(vowel-at-index? text 1))
(string-append (substring text 1)
(substring text 0 1)
"ay"))
; case 2.2
(else
(string-append (substring text 2)
(substring text 0 2)
"ay"))))
(define (pig-latin lst)
(string-join (map pigify lst)))
For the final step, we only need to apply the pigify procedure to each element in the input, and that's what map does. It works as expected:
(pig-latin '("this" "is" "a" "crazy" "exercise"))
=> "isthay isway away azycray exerciseway"

clojure - avoid extra whitespace when combining string and variable

I'm writing a program that uses printl-str to return commands of an assembly language. I need to use variables in my code and I'm having this issue where the function will return extra whitespace where I don't want it:
(defn pushConstant [constant]
(println-str "#" constant "\r\nD=A\r\n#SP\r\nA=M\r\nM=D\r\n#SP\r\nM=M+1"))
Where instead of having, assuming that constant = 17
#17
D=A
#SP
A=M
M=D
#SP
M=M+1
I'm having:
# 17
D=A
#SP
A=M
M=D
#SP
M=M+1
Which is problematic for my assembly code. I have this issue in so many cases like this. I'll be glad to hear advice on how to avoid this extra whitespace between the String and the variable.
Frankly, I'd implement that to look more like the following:
(defn pushConstant [constant]
(->> [(str "#" constant)
"D=A"
"#SP"
"A=M"
"M=D"
"#SP"
"M=M+1"]
(interpose "\r\n")
(apply str)))
That way you don't have one big ugly format string, but break down your operations into small, readable pieces.
That said, the piece that makes a difference for you here is (str "#" constant), combining your # with the argument with no added whitespace.
Create the string using str which only concatenates (println interleaves spaces):
(defn pushConstant [constant]
(println-str (str "#" constant "\r\nD=A\r\n#SP\r\nA=M\r\nM=D\r\n#SP\r\nM=M+1")))

How do I check whether a string is UpperCase or not in clojure?

I want to check whether a string is uppercase or not. There is a function to check for a character but no function to check it for a string.
For this kind of question, start by looking for the Java answer :)
Is there an existing library method that checks if a String is all upper case or lower case in Java?
In this case, my recommandation is to use Apache Commons Lang's StringUtils/isAllUpperCase
(import org.apache.commons.lang3.StringUtils)
(StringUtils/isAllUpperCase "HeLLO")
If you want a solution portable across all platforms (Clojure, ClojureScript, ...) the best strategy is probably to compare the uppercased string to the original:
(require '[clojure.string :as str])
(defn all-uppercase? [s]
(= s (str/upper-case s)))
Assuming that you want to check that every character in String is uppercase, you can use every? like this:
user=> (every? #(Character/isUpperCase %) "Hello")
false
user=> (every? #(Character/isUpperCase %) "HELLO")
true
The cuerdas library for Clojure(Script) has a function upper (and locale-upper) which could be used to check for uppercase.
https://funcool.github.io/cuerdas/latest/#upper
#(= % (cuerdas/upper %))

Lisp - Displaying a String to List

I've been looking for a way to convert user input (read-line) to a list of atoms that I can manipulate more easily.
For example:
SendInput()
This is my input. Hopefully this works.
and I want to get back..
(This is my input. Hopefully this works.)
Eventually it'd be ideal to remove any periods, commas, quotes, etc. But for now I just wanna store the users input in a list (NOT AS A STRING)
So. For now i'm using
(setf stuff (coerce (read-line) 'list))
and that returns to me as...
(#\T #\h #\i #\s #\Space #\i #\s #\Space #\m #\y #\Space #\i #\n #\p #\u #\t #. #\Space #\H #\o #\p #\e #\f #\u #\l #\l #\y #\Space #\t #\h #\i #\s #\Space #\w #\o #\r #\k #\s #.)
So now i'm on the hunt for a function that can take that list and format it properly...
Any help would be greatly appreciated!
Rainer's answer is better in that it's a bit more lightweight (and general), but you could also use CL-PPCRE , if you already have it loaded (I know I always do).
You can use SPLIT directly on the string you get from READ-LINE, like so:
(cl-ppcre:split "[ .]+" (read-line))
(Now you have two problems)
What you want to do is to split a sequence of characters (a String) into a list of smaller strings or symbols.
Use some of the split sequence functions available from a Lisp library (see for example cl-utilities).
In LispWorks, which comes with a SPLIT-SEQUENCE function) I would for example write:
CL-USER 8 > (mapcar #'intern
(split-sequence '(#\space #\.)
"This is my input. Hopefully this works."
:coalesce-separators t))
(|This| |is| |my| |input| |Hopefully| |this| |works|)
Remember, to get symbols with case preserving names, they are surrounded by vertical bars. The vertical bars are not part of the symbol name - just like the double quotes are not part of a string - they are delimiters.
You can also print it:
CL-USER 19 > (princ (mapcar #'intern
(split-sequence '(#\space #\.)
"This is my input. Hopefully this works."
:coalesce-separators t)))
(This is my input Hopefully this works)
(|This| |is| |my| |input| |Hopefully| |this| |works|)
Above prints the list. The first output is the data printed by PRINC and the second output is done by the REPL.
If you don't want symbols, but strings:
CL-USER 9 > (split-sequence '(#\space #\.)
"This is my input. Hopefully this works."
:coalesce-separators t)
("This" "is" "my" "input" "Hopefully" "this" "works")

Resources