Hi,
I have to loop through a string, consecutively replacing substrings with single characters, one substring at a time. Each time through the loop, I need to use a cond to check that the length of the last replaced string, is shorter than the length of the previous replaced string.
So, as an artificial example, say you have a string "1001110011011", and a map or vector of substrings and replacements
( def map_translations
{
{ "00" "0" },
{ "01" "0" },
{ "10" "0" },
{ "11" "1" }
}
( The resemblance to a conjunction truth table is coincidental. This is the simplest example I could think of. )
So I want to loop through the map of translations several times, each time trying to simplify the original expression further, until a result of either "-1" or a single character is returned.
If the length of the replaced string is greater than or equal to the length of the last replacement, then I'd want to return a "-1" string.
But I can't figure out, how to compare the length of a result of a recur assignment, to the length of the PREVIOUS recur assignment.
So, it might look something like this (untested):
( defn simplify-string
[ s_original-string s_substring s_replacement-character ]
(
let [ s_simplified-string ( clojure.string/replace s_original-string s_substring s_replacement-character ) ]
s_simplified-string
)
)
( defn simplify-dat-guy
"Loops through a map of substring to character translations,
attempting to simplify a given expression.
e.g., '00101' through successive translations would become a shorter form
"
[ s_exp ]
( loop
[ [k v] map_longform-shortform-translation s_simplified-expression s_exp ]
( recur [ k v ] ( simplify-string s_simplified-expression k v ) )
)
)
( defn completely-simplify-dat-guy
"Loops through simplify-dat-guy several times,
until either a single character or a "-1" is returned.
e.g., '00101' through successive simplify-dat-guys would eventually become "0".
"
[ s_exp ]
( loop
[ s_simplified-expression s_exp ]
(
cond
( = ( .length s_simplified-expression ) 1 )
s_simplified-expression
( comment
I want to put a second condition in here, something like
( = ( .length s_simplified-expression ) ( .length PREVIOUS_s_simplified_expression ) )
"-1"
)
( recur ( simplify-dat-guy s_simplified-expression ) )
)
)
)