Opa: What Is the Fastest Way to Reverse a String? - recursion

What is the most performant way to implement a "reverse a string" function in Opa?
I am aware of String.reverse(). It does get the job done, but its source code is bound to some external function. I want to understand what the best (fastest) tools are for reordering a string.
So, for example, this works, but isn't particularly fast:
reverse(s:string) =
p = parser
| x=(.) xs=(.*) -> Text.concat(reverse(Text.to_string(xs)), x)
| x=(.*) -> x
Parser.parse(p, s)
How can I make this perform better?
Edit: Another Implementation Based on akoprowski's Answer
I think this is what he has in mind. This one is much faster than the parser version, but the built-in String.reverse is the way to go if all you need to do is reverse a string.
reverse_list(l:list(string)):list(string) =
match l with
| [x|xs] -> reverse_list(xs) ++ [x]
| _ -> []
reverse_string(s:string) =
string_list = List.init((i -> String.get(i, s)), String.length(s))
String.flatten(reverse_list(string_list))
Edit: The Above Implementation Is Not Ideal
Check out the accepted answer for a better option.

Why don't you want to use String.reverse? (which is the preferred way of reversing a string). Or is that just an exercise?
I'd suggest using List.init to generate a list of characters, String.get (to get n'th character of the string) and String.flatten to convert a list of characters back to a string.
That should be faster than the parsing-based approach, which btw. has quadratic complexity so no wonder it's slow.
Edit: the solution I had in mind:
reverse_string(s:string) =
n = String.length(s)
rev_list = List.init((i -> String.get(n - i - 1, s)), n)
String.flatten(rev_list)

Related

Julia: select all except a few indices [duplicate]

I have an array a=rand(100), I want to get every value except the values at the indices notidx=[2;50]. Is there a clean way to get a at the other values? I am looking for a good way to do both a copy and a view.
Currently I make the array [1;3:49;51:100] by symdiff(1:100,notidx), but a[symdiff(1:length(a),notidx)] and view(a,a[symdiff(1:length(a),notidx)]) are not very clean (or understandable to others) ways of doing this.
I don't have anything super clean, but you can do
a[setdiff(1:end, notidx)]
which is slightly cleaner than what you had, or
ind = trues(length(a))
ind[notidx] = false
a[ind]
which is pretty verbose but very clear.
Update:
If you are using julia-v0.5+, you can also use the new generator expression, for example:
view(a, [i for i in indices(a)... if i ∉ notidx])
and
[a[i] for i in indices(a)... if i ∉ notidx]
Old post:
To get a copy, you can firstly make a copy of a, then manipulate it with deleteat! to delete those values at specific indices. After you've done this, it's convenient to get a view of a via indexin:
a = rand(100)
# => 100-element Array{Float64,1}:
0.62636
0.488919
0.499884
....
b = copy(a)
deleteat!(b, [2,50])
# => 98-element Array{Float64,1}:
0.62636
0.499884
....

F# collection type for mixed types

This question is coming from someone who is working on making the transition from R to F#. I fully acknowledge my approach here may be wrong so I am looking for the F# way of doing this. I have a situation where I want iterate through a set of XML files, parse them, and extract several values to identify which ones need further processing. My natural inclination is to Map over the array of XML data, exampleData in this case, parse each using the RawDataProvider type provider, and finally create a Map object for each file containing the parsed XML, the Status value from the XML, and the ItemId value.
Turns out that the Map type in F# is not like a List in R. Lists in R are essentially hashmaps which can support having mixed types. It appears that the Map type in F# does not support storing mixed types. I have found this to be incredibly useful in my R work and am looking for what the right F# collection is for this.
Or, am I thinking about this all wrong? This is a very natural way for me to process data in R so I would expect there would be a way to do it in F# as well. The assumption is that I am going to do further analysis and add additional elements of data to these collections.
Update:
This seems like such a simple use case that there must be an idiomatic way of doing this in F# without having to define a Record type for each step of the analysis. I have updated my example to further illustrate what I am trying to do. I want to return an Array of the Map objects that I have analyzed:
type RawDataProvider = XmlProvider<"""<product Status="Good" ItemId="123" />""">
let exampleData = [| """<product Status="Good" ItemId="123" />"""; """<product Status="Bad" ItemId="456" />"""; """<product Status="Good" ItemId="789" />"""|]
let dataResult =
exampleData
|> Array.map(fun fileData -> RawDataProvider.Parse(fileData))
|> Array.map(fun xml -> Map.empty.Add("xml", xml).Add("Status", xml.Status).Add("ItemId", xml.ItemId))
|> Array.map(fun elem -> elem.["calc1Value"] = calc1 elem["itemId"])
|> Array.map(fun elem -> elem.["calc2"] = calc2 elem.["ItemId"] elem.["calc1Value"])
This is what I would consider almost idiomatic here - I'm keeping the same shape as in your example so you can match the two:
let dataResult =
exampleData
|> Array.map(fun fileData -> RawDataProvider.Parse(fileData))
|> Array.map(fun xml -> xml, calc1 xml.ItemId)
|> Array.map(fun (xml, calcedValue1) -> xml, calcedValue1, calc2 xml.ItemId calcedValue1)
What XmlProvider really gives you is not simply xml parsing, but the fact that it generates a strongly typed representation of the xml. This is better than putting the data in a map, in that it gives you stronger guarantees about whether your program is doing the right thing. For instance it wouldn't let you mix up itemId and ItemId as it happened in your code snippet ;)
For the values you calculate in the following steps, you could use tuples instead of a record. In general, records are preferred to tuples as they lead to more readable code, but combining related values of different types into ad-hoc aggregates is really the scenario where using tuples shines.
Now, I said almost idiomatic - I would break up parsing and processing parsed xmls into separate functions, and calculate both calc1 and calc2 results in a single function instead of composing two Array.maps like this:
let dataResult =
parsedData
|> Array.map(fun xml ->
let calced1 = calc1 xml.ItemId
xml, calced1, calc2 xml.ItemId calced1)
If you're coming from R background, you might want to check out Deedle for an alternative approach. It gives you a workflow similar to R in F#.

Tail Recursions in erlang

I'm learning Erlang from the very basic and have a problem with a tail recursive function. I want my function to receive a list and return a new list where element = element + 1. For example, if I send [1,2,3,4,5] as an argument, it must return [2,3,4,5,6]. The problem is that when I send that exact arguments, it returns [[[[[[]|2]|3]|4]|5]|6].
My code is this:
-module(test).
-export([test/0]).
test()->
List = [1,2,3,4,5],
sum_list_2(List).
sum_list_2(List)->
sum_list_2(List,[]).
sum_list_2([Head|Tail], Result)->
sum_list_2(Tail,[Result|Head +1]);
sum_list_2([], Result)->
Result.
However, if I change my function to this:
sum_list_2([Head|Tail], Result)->
sum_list_2(Tail,[Head +1|Result]);
sum_list_2([], Result)->
Result.
It outputs [6,5,4,3,2] which is OK. Why the function doesn't work the other way around([Result|Head+1] outputing [2,3,4,5,6])?
PS: I know this particular problem is solved with list comprehensions, but I want to do it with recursion.
For this kind of manipulation you should use list comprehension:
1> L = [1,2,3,4,5,6].
[1,2,3,4,5,6]
2> [X+1 || X <- L].
[2,3,4,5,6,7]
it is the fastest and most idiomatic way to do it.
A remark on your fist version: [Result|Head +1] builds an improper list. the construction is always [Head|Tail] where Tail is a list. You could use Result ++ [Head+1] but this would perform a copy of the Result list at each recursive call.
You can also look at the code of lists:map/2 which is not tail recursive, but it seems that actual optimization of the compiler work well in this case:
inc([H|T]) -> [H+1|inc(T)];
inc([]) -> [].
[edit]
The internal and hidden representation of a list looks like a chained list. Each element contains a term and a reference to the tail. So adding an element on top of the head does not need to modify the existing list, but adding something at the end needs to mutate the last element (the reference to the empty list is replaced by a reference to the new sublist). As variables are not mutable, it needs to make a modified copy of the last element which in turn needs to mutate the previous element of the list and so on. As far as I know, the optimizations of the compiler do not make the decision to mutate variable (deduction from the the documentation).
The function that produces the result in reverse order is a natural consequence of you adding the newly incremented element to the front of the Result list. This isn't uncommon, and the recommended "fix" is to simply list:reverse/1 the output before returning it.
Whilst in this case you could simply use the ++ operator instead of the [H|T] "cons" operator to join your results the other way around, giving you the desired output in the correct order:
sum_list_2([Head|Tail], Result)->
sum_list_2(Tail, Result ++ [Head + 1]);
doing so isn't recommended because the ++ operator always copies it's (increasingly large) left hand operand, causing the algorithm to operate in O(n^2) time instead of the [Head + 1 | Tail] version's O(n) time.

Trying to understand the SML option structure

Okay so I started learning SML for a class, and I'm stuck with the option structure.
What I have so far for this example:
datatype suit = spades|hearts|clubs|diamonds;
datatype rank = ace|two|three|...|j|q|k|joker;
type card = suit*rank;
My lecturer was trying to explain the use of the option structure by saying that not all cards necessarily have a suit; jokers don't have a suit associated with them.
So when designing a function getsuit to get the suit of a card, we have the following:
datatype 'a option = NONE | SOME of 'a;
fun getsuit ((joker,_):card):suit option = NONE
| getsuit ((_,s):card):suit option = SOME s;
But using emacs, I get two errors, one saying how the pattern and constraint don't agree,
pattern: rank * ?.suit
constraint: rank * suit
and the other saying how the expression type and the resulting types don't agree.
expression: ?.suit option
result type: suit option
This was the code provided by the lecturer so clearly they're not of much help if it results in errors.
What's the meaning of "?." and why does it show up? How would I correctly define this function?
Not really a problem with option as you've defined it.
You've got the order of suit and rank in your card patterns the wrong way:
Try:
datatype 'a option = NONE | SOME of 'a;
fun getsuit ((_, joker):card):suit option = NONE
| getsuit ((s, _):card):suit option = SOME s;
My version of ML probably prints errors differently so I'm not sure how to explain the the meaning of ?. etc. But it's simple enough if you take it bit by bit:
Try
(clubs, ace);
The interpreter (or emacs if that's what you're using) tells you the type is a product of a suit * rank. That's ML's type inference at work, but you can specify the type (you expect) like this:
(clubs, ace): suit*rank;
Or
(clubs, ace): card; (* Works, since card is defined as (suit*rank) *)
And you won't have any complaints. But obviously you would if you did
(clubs, ace): rank*suit;
Or
(clubs, ace): card; (* card is defined as (rank*) *)
You placed a constraint on the type of getsuit's argument (that it must be a card, or the compatible (suit*rank) product), but the pattern is of type (rank*?) or (?*rank), neither of which are compatible with (suit*rank).

Haskell function to get part of date as string

I have a beginner question about dates and String in Haskell.
I need to get part of date (year, month or day) as String in Haskell. I found out, that if I write the following two lines in GHCi
Prelude> now <- getCurrentTime
Prelude> let mon = formatTime defaultTimeLocale "%B" now
then mon is of type String. However, I am unable to put this in a function. I tried for instance the following:
getCurrMonth = do
now <- getCurrentTime
putStrLn (formatTime defaultTimeLocale "%B" now)
But this returns type IO () and I need String (also not IO String, only String).
I understand that do statement creates a monad, which I don't want, but I have been unable to find any other solution for getting date in Haskell.
So, is there any way to write a function like this?
Thanks in advance for any help!
If you want to return a String representing the current time, it will have to be in the IO monad, as the value of the current time is always changing!
What you can do is to return a String in the IO monad:
> getCurrMonth :: IO String
> getCurrMonth = do
> now <- getCurrentTime
> return (formatTime defaultTimeLocale "%B" now)
then, from your top level (e.g. in main), you can pass the String around:
> main = do
> s <- getCurrMonth
> ... do something with s ...
If you really want a pure function of that sort, then you need to pass in the time explicitly as a parameter.
import System.Locale (defaultTimeLocale)
import System.Time (formatCalendarTime, toUTCTime, getClockTime, ClockTime)
main = do now <- getClockTime
putStrLn $ getMonthString now
getMonthString :: ClockTime -> String
getMonthString = formatCalendarTime defaultTimeLocale "%B" . toUTCTime
Notice how getMonthString can be pure since the IO action getClockTime is performed elsewhere.
I used the old-time functions, because I was testing it out on codepad, which apparently doesn't have the newer time package. :( I'm new to the old time functions so this might be off a couple hours since it uses toUTCTime.
As Don said, there's no way to avoid using monads in this situation. Remember that Haskell is a pure functional language, and therefore a function must always return the same output given a particular input. Haskell.org provides a great explanation and introduction here that is certainly worth looking at. You'd also probably benefit from monad introduction like this one or a Haskell I/O tutorial like this one. Of course there are tons more resources online you can find. Monads can initially be daunting, but they're really not as difficult as they seem at first.
Oh, and I strongly advise against using unsafePerformIO. There's a very good reason it has the word "unsafe" in the name, and it was definitely not created for situations like this. Using it will only lead to bad habits and problems down the line.
Good luck learning Haskell!
You can't get just a String, it has to be IO String. This is because getCurrMonth is not a pure function, it returns different values at different times, so it has to be in IO.

Resources