Will cffi:with-foreign-object free array elements? - memory-leaks

Here's what I'm trying to do:
(defun do-some-fun-stuff (string-list)
(let ((n (length string-list)))
(cffi:with-foreign-object (array :string n)
(loop for i below n
for s in string-list
do (setf (cffi:mem-aref array :string i) s))
(call-some-foreign-function n array))))
I understand that array itself is going to be freed upon leaving the with-... block, but how about the elements of the array? The (setf (cffi:mem-aref array :string i) s) expression will allocate a null-terminated char* array for each element, right? Should I free them myself or CFFI will do that for me? I couldn't find a definitive answer neither in the documentation nor in the CFFI code.

Related

How do I find the number of characters in a string using scheme programming language?

I used string-length to get the number of characters but I am having difficulties in defining a recursive function. Should I convert the string to a list and then count the elements?
There's no useful way of doing this recursively (or even tail recursively): strings in Scheme are objects which know how long they are. There would be such an approach in a language like C where strings don't know how long they are but are delimited by some special marker. So for instance if (special-marker? s i) told you whether the i'th element of s was the special marker object, then you could write a function to know how long the string was:
(define (silly-string-length s)
(let silly-string-length-loop ([i 1])
(if (special-marker? s i)
(- i 1)
(silly-string-length-loop (+ i 1)))))
But now think about how you would implement special-marker? in Scheme: in particular here's the obvious implementation:
(define (special-marker? s i)
(= i (+ (string-length s) 1)))
And you can see that silly-string-length is now just a terrible version of string-length.
Well, if you wanted to make it look even more terrible, you could, as you suggest, convert a string to a list and then compute the length of the lists. Lists are delimited by a special marker object, () so this approach is reasonable:
(define (length-of-list l)
(let length-of-list-loop ([i 0]
[lt l])
(if (null? lt)
i
(length-of-list-loop (+ i 1) (rest lt)))))
So you could write
(define (superficially-less-silly-string-length s)
(length-of-list
(turn-string-into-list s)))
But, wait, how do you write turn-string-into-list? Well, something like this perhaps:
(define (turn-string-into-list s)
(let ([l (string-length s)])
(let loop ([i 0]
[r '()])
(if (= i l)
(reverse r)
(loop (+ i 1)
(cons (string-ref s i) r))))))
And this ... uses string-length.
What is the problem with?
(string-length string)
If the question is a puzzle "count characters in a string without using string-length",
then maybe:
(define (my-string-length s)
(define (my-string-length t n)
(if (string=? s t) n
(my-string-length
(string-append t (string (string-ref s n))) (+ n 1))))
(my-string-length "" 0))
or:
(define (my-string-length s)
(define (my-string-length n)
(define (try thunk)
(call/cc (lambda (k)
(with-exception-handler (lambda (x)
(k n))
thunk))))
(try (lambda ()
(string-ref s n)
(my-string-length (+ n 1)))))
(my-string-length 0))
(but of course string-ref will be using the base string-length or equivalent)

Want more Clojure way to brute force string match

I've implementing a brute force string matching algorithm in Clojure. It works as it should, but what I'm looking for is how to make this code "cleaner," and more readable. Note that I also have to have the algorithm print out how it's doing the character comparisons. I don't know about all the conventions to pay attention to, and I'd really like some tips on how to write Clojure better.
What it does: It takes a piece of text, and for each of it's indexes (since the text is of a type String), match it with the input string. If there's a match, we compare the second character to the next index of the text. It's a lot to explain in English, but if you run the program, it prints out what it's doing.
The code:
(defn underscores [n]
(apply str (repeat n "_")))
(defn brute_force_string_match
"Receives text as string type as its first argument,
string in second argument, brute force matches the
string to the text. Assumes text is longer than string."
[text
string]
;; for loop
;; i is 1 less than the amount of No matches you will get
(loop [i 0
j_and_matches [0 0]]
;;outer loop stops when i > n -m
(if (and
(<= i (- (count text) (count string)))
(not= (j_and_matches 0) (count string)))
;; the "while loop"
(do
(println "")
(print "\nPos = " i "\n"text"\n"
(str (underscores i) string))
(recur
(inc i)
(loop [j 0
print_pos i
undscore_amt 0
matches (j_and_matches 1)]
(if (and
(< j (count string))
(= (.charAt string j) (.charAt text (+ i j))))
(do
(print "\n" (str (str (underscores print_pos)) "^ Match! "))
(recur (inc j)
(inc print_pos)
(inc undscore_amt)
(inc matches)))
(do
(if (not= j (count string))
(print "\n" (str (str (underscores print_pos)) "^ No Match ")))
[j matches])))))
(if (= (j_and_matches 0) (count string))
(do (println "\n Pattern found at position " (dec i))
(println "The number of comparisons: " (+ (j_and_matches 1) (dec i)))
(dec i))
-1))))
For one thing, j has no place in your outer loop, which has i go through the possible start points for a match. j is the local index into string while testing it against the text starting from i.
I'd write it something like this:
(defn brute_force_string_match [text string]
(let [last-start (- (count text) (count string))]
(loop [i 0, matches []]
(if (> i last-start)
matches
(let [match?
(loop [j 0]
(or (= j (count string))
(and (= (.charAt string j) (.charAt text (+ i j)))
(recur (inc j)))))]
(recur (inc i) (if match? (conj matches i) matches)))))))
Apart from the superfluous j in your outer loop, I don't know that yours is significantly different from this.
As a matter of style,
I've bound the complex inner loop expression to local match?. This keeps the final
line clear.
I've used and and or to simplify the conditionals in that expression.
I've pushed down the conditional (if match? ... ) into an argument of the
outer recur.
Note
You can use get instead of .charAt to access characters of strings.

Scheme list of strings

my function in scheme looks like this
(define (func1 input)
(let kloop ((x 6))
(let ((act (string-copy (func2 input2))))
(if (eq? act "") (display "null") (display act))
(if (> x 0) (kloop (- x 1)))))))
func2 return some string which is stored in act. Now I have to create a list of all strings returned by this function. Here above, I am just displaying those strings. I tried different approaches, but nothing is working out. I tried using append and cons.
Please suggest.
Your last if is missing the else case, which is where one would expect the return value of the function to be.
You don't mention how you've tried to use append and cons, but a common pattern is to pass an accumulating parameter around in the loop:
(define (five input)
(let loop ((x 5) (outputs '()))
(if (> x 0)
(loop (- x 1) (cons input outputs))
outputs)))
> (five "yes")
'("yes" "yes" "yes" "yes" "yes")
You are calling func2 on input six times. Does it return a different value each time? If not, this works:
(define (func1 input)
(make-list 6 (func2 input)))
The question is a bit confusing, you should provide a sample of the expected output for a given input. And why the empty string is treated differently in your code? apparently the recursion should advance on the value of x, not the value of the string returned by func2. Also, why are you copying the string? seems unnecessary.
Assuming that the named let is used just for keeping track of the number of iterations, this solution seems aligned with your intent, as this will return a 6-element list of all strings returned by func2
(define (func1 input)
(let kloop ((x 6))
(if (zero? x)
'()
(cons (func2 input)
(kloop (- x 1))))))
But we can be smarter and use the named let to give a tail-recursive solution, which is more efficient:
(define (func1 input)
(let kloop ((x 6)
(acc '()))
(if (zero? x)
acc
(kloop (- x 1)
(cons (func2 input)
acc)))))

detecting a palindrome without using reverse

I was thinking a way to create a function that detects a palindrome without using reverse...
I thought I would be clever and do a condition where substring 0 to middle equals substring end to middle. I;ve found out that it only works on words with 3 letters "wow" because "w" = "w". But if the letters are like "wooow", wo doesn't equal ow. What is a way to detect palindrome without using a reverse function?
Hint or solutions might be very helpful
(define (palindrome? str)
(cond
((equal? (substring str 0 (- (/ (string-length str) 2) 0.5))
(substring str (+ (/ (string-length str) 2) 0.5) (string-length str))) str)
(else false)))
Oh and I'm using beginner language so I can't use stuff like map or filter
yes I know this is a very useless function haha
It's possible to solve this problem by messing around with the string's characters in a given index. The trick is to use string-ref wisely. Here, let me give you some hints pointing to a solution that will work with the beginner's language :
; this is the main procedure
(define (palindrome? s)
; it uses `loop` as a helper
(loop <???> <???> <???>))
; loop receives as parameters:
; `s` : the string
; `i` : the current index, starting at 0
; `n` : the string's length
(define (loop s i n)
(cond (<???> ; if `i` equals `n`
<???>) ; then `s` is a palindrome
(<???> ; if the char at the current index != its opposite (*)
<???>) ; then `s` is NOT a palindrome
(else ; otherwise
(loop <???> <???> <???>)))) ; advance the recursion over `i`
Of course, the interesting part is the one marked with (*). Think of it, a string is a palindrome if the char at the 0 index equals the char at the n-1 index (n being the string's length) and the char at the 1 index equals the char at the n-2 index, and so on. In general, if it's true that the char at the i index equals the char at the n-i-1 index (its "opposite") for all i, then we can conclude that the string is a palindrome - but if a single pair of opposite chars is not equal to each other, then it's not a palindrome.
As a further optimization, notice that you don't need to traverse the whole string, it's enough to test the characters up to the half of the string's length (this is explained in Chris' answer) - intuitively, you can see that if the char at i equals the char at n-i-1, then it follows that the char at n-i-1 equals the char at i, so there's no need to perform the same test two times.
Try to write the procedures on your own, and don't forget to test them:
(palindrome? "")
=> #t
(palindrome? "x")
=> #t
(palindrome? "axa")
=> #t
(palindrome? "axxa")
=> #t
(palindrome? "axc")
=> #f
(palindrome? "axca")
=> #f
(palindrome? "acxa")
=> #f
(palindrome? "axcta")
=> #f
Here is a creative answer
(define (palindrome list)
(let halving ((last list) (fast list) (half '()))
(cond ((null? fast) (equal? half last))
((null? (cdr fast)) (equal? half (cdr last)))
(else (halving (cdr last) (cddr fast)
(cons (car last) half))))))
It travels halfway down the list (using fast to find the end), builds up a list of the first half and then simply uses equal? on half with the remainder of list.
Simple.
For each i, from 0 to floor(length / 2), compare the character at index i and at index length - i - 1.
If mismatch, return false.
Otherwise, if the loop runs out, return true.
Skeletal code:
(define (palindrome? str)
(define len (string-length str))
(define halfway <???>)
(let loop ((i 0))
(cond ((>= i halfway) #t)
((char=? <???> <???>)
(loop (+ i 1)))
(else #f))))

Byte collection to string on clojure

The following code
(defn caesar-block-cypher
"Computes the caesar block cypher for the given text with the k key. Returns an array of bytes"
[k text]
(let [byte-string (.getBytes text)]
(loop [return-byte-array [] byte-string byte-string]
(if (= '() byte-string)
return-byte-array
(recur
(conj return-byte-array (byte (+ k (first byte-string))))
(rest byte-string))))))
Returns an array of bytes after processing the caesar cipher with key k at text. I want to convert back the byte array to a string or perform the cipher over the string directly, but (new String return-byte-array) doesn't work. Any suggestions?
EDIT: Thanks for the responses. I recodified this on a more functional style (that actually works):
(defn caesar-block-cypher
"Computes the caesar block cypher for the given text with the k key."
[k text & more]
(let [byte-string (.getBytes (apply str text (map str more)))]
(apply str (map #(char (mod (+ (int %) k) 0x100)) byte-string))))
(let [byte-array (caesar-block-cypher 1 "Hello, world!")]
(apply str (map char byte-array)))
Use java's String constructor to create string quickly like this,
(let [b (caesar-block-cypher 1 "Hello World")]
(String. b))
You can use slurp, it also works for byte arrays:
From https://clojuredocs.org/clojure.core/slurp#example-588dd268e4b01f4add58fe33
;; you can read bytes also
(def arr-bytes (into-array Byte/TYPE (range 128)))
(slurp arr-bytes)
AFAIK ceaser chipher just shifts chars why are you dealing with bytes,
(let [s "Attack"
k 1
encoded (map #(char (+ (int %) k)) s)
decoded (map #(char (- (int %) k)) encoded)]
(apply str decoded))

Resources