I'm new to scheme.
I'm wondering how to sort strings in scheme.
Now I become to think that using [string=?][string?] will help me to compare contents of the list.
However, I'm still struggling with sort it out, literally.
Does anyone help me to implement this?
Most Scheme implementations have a builtin sort that can sort lists:
> (sort '("How" "to" "sort" "strings" "in" "Scheme") string<?)
'("How" "Scheme" "in" "sort" "strings" "to")
If your implementation complain that sort is an unbound identifier, then lookup sort in the documentation to see how to include it.
Note that the predicate string<? is a function that compares two strings.
Using < instead you can sort lists of numbers.
Related
For instance, "tween" is a substring of "between" but "teen" is not. Basically, I am looking for a function like strstr in libc.
There is a strstr function in the ATS prelude as well. See a working example with all common combinations of the "haystack" and "needle" strings.
If you target JavaScript, please use the function string_indexOf.
It takes two strings haystack and key; if it returns -1, then key does not occur in haystack; otherwise, it returns a non-negative integer referring to the position of the leftmost occurrence of key in haystack.
Basically, what I want is a function of the following interface:
fun stringlst2string (list0(string)): string
where stringlst2string takes a list of string and returns the concatenation of them.
One can use the standard library function stringlst_concat to accomplish this. See the reference for explanation and this snippet for a working example.
One can readily do this kind of thing (that is, turning one form of sequence into another form of sequence) by going through linear streams. For instance, the following code turns a list of strings into a stream of strings and then into a stream of streams (of chars) and then into a stream of chars and then into a string:
fun
stringlst2string
(
xs: list0(string)
) : string =
strptr2string
(
string_make_stream_vt
(
stream_vt_concat
((streamize_list0_elt(xs)).map(TYPE{stream_vt(charNZ)})(lam x => streamize_string_char(x)))
)
)
This is a very lean implementation in terms of memory usage, and there is no memory that is not released at the end (except for the memory needed to store the returned string). Clearly, the same approach applies if you want to concatenate an array of strings.
I used read to get a line from a file. The documentation said read returns any, so is it turning the line to a string? I have problems turning the string "1" to the number 1, or "500.8232" into 500.8232. I am also wondering if Racket can directly read numbers in from a file.
Check out their documentation search, it's complete and accurate. Conversion functions usually have the form of foo->bar (which you can assume takes a foo and returns a bar constructed from it).
You sound like you're looking for a function that takes a string and returns a number, and as it happens, string->number does exist, and does pretty much exactly what you're looking for.
Looks like this was answered in another question:
Convert String to Code in Scheme
NB: that converts any s-expression, not just integers. If you want just integers, try:
string->number
Which is mentioned in
Scheme language: merge two numbers
HTH
I'm trying to create a map in Go that is keyed by big integers. Effective Go explicitly says that:
Structs, arrays and slices cannot be used as map keys, because equality is not defined on those types.
which makes sense. I could of course convert the big integers to strings and use the string as a key, but I'm looking for a more general solution here. Can I wrap my structure into something (an interface?) that implements an equality function and use that instead?
Example code that, of course, doesn't work:
package main
import (
"big"
"fmt"
)
func main() {
one1 := big.NewInt(1)
one2 := big.NewInt(1)
hmap := make(map[*big.Int] int)
hmap[one1] = 2
_, exists := hmap[one2]
fmt.Printf("Exists=%v\n", exists)
}
The rules about equality are going to change soon. From the Go 1 plan:
Go 1 will define equality on struct and array values composed from
fields on which equality is also defined (element-wise comparison).
It will remove equality on function and map values except for
comparison with nil. Go 1 will continue to exclude equality for
slices. (In the general case it is infeasible.)
However, even with this rules, you can not use *BigInt as key, since it contains a slice. Also note, that it is not possible in Go to write a custom equality operator (neither it is possible to override any other operator). But that's actually a strength of Go in my opinion - things are just simpler without it.
So, you will have to use strings for your keys. The strings however do not need to be formatted in decimal (or any other format) as long as you do not want to print them. So the fastest way is probably to use the Bytes() method (which will also discard the sign, make sure to include that separately in your string).
I have a SQLite-backed core data storage and would like to fetch a list of managed objects using NSFetchRequest. I want said list to be sorted by a boolean value that can be easily calculated at the database level. I know this because it’s possible to formulate the same conditions using an NSPredicate, which would look as follows:
[NSPredicate predicateWithFormat:#"uid = %#", currentUID]
Sadly, there seems to be no way to formulate a condition like this using an NSSortDescriptor. How do I best go about this? Do I fetch two lists, one with
[NSPredicate predicateWithFormat:#"uid = %#", currentUID]
and one with
[NSPredicate predicateWithFormat:#"uid != %#", currentUID]
and combine them later on? Can I then still elegantly use a NSFetchedResultsController?
Or should I fetch all items and sort them later in code? Or is there anything I’ve missed.
Just create an array (even if it contains only a single element) of NSSortDescriptors to sort the result as desired.
You use setSortDescriptors: to set them.
The fetch can handle both predicates and sorting at the same time.
What you're asking for doesn't make sense.
A predicate is used to select which objects should be returned in a set of results. This is determined by evaluating the predicate against each object to see if the predicate evaluates to YES or NO (a binary value, or one of 2 possible values). If it evaluates to YES, then the object is included. If it evaluates to NO, then the object is excluded. There is no middle ground.
By contrast, a sort descriptor evaluates to less than, equal, or greater than. In other words, it is a ternary value (it can be one of 3 things). There is no way to express a ternary value with a predicate (since a predicate is binary), and so using a predicate as a sort descriptor makes absolutely no sense. The API doesn't allow it, because logic doesn't allow it.
So the real question is: what are you trying to do? Why do you think you need a predicate in your sort descriptors?