How would i replace spaces in a string with “%20", recursively?











up vote
2
down vote

favorite












So I've figured this out non-recursively like this:



noSpaces :: [Char] -> [Char]    
noSpaces xs = [f | r <- xs, f <- if (r == ' ') then "%20" else [r]]


but now i need to figure out how to do that recursively, with no pattern matching and no concatenation...



I figured out other recursive functions (gcd, fib, and a couple others involving numbers) easily enough... but am lost on this for some reason and getting lost-er the more stuff i try that doesn't work...



I found the other answer on here about doing this in Haskell... but that idea uses two separate functions, one being like a helper function -- how would you go about this all in one function, as concisely as possible?



something like:



replace [Char] = [Char]
replace (x:xs)
| x == ' ' = "%20":replace xs


which compiles but of course i get the 'non-exhaustive patterns in function' error message, and can't figure out what to put in the 'otherwise' clause...



any help would be appreciated!










share|improve this question


















  • 1




    If x is a space, it should become "%20". You have that part mostly right (: should be ++). If it’s not a space, it should become what, considering what you have available in the function?
    – Ry-
    Nov 9 at 9:33












  • I imagine you have to do it recursively as an exercise for class. Just for your edification, the way you have done it with a list comprehension is quite nice; a recursive implementation of this function would be awkward "in the wild".
    – luqui
    Nov 9 at 21:24










  • Thank you luqui, I do appreciate that. And yes, this is for a class -- but not for the actual graded assignment, just the "working through the tutorial" part. I just hate leaving something un-figured-out, when it's implied there is, indeed, a solution. Thank you Ry for making me think.
    – Stormy
    Nov 10 at 23:24















up vote
2
down vote

favorite












So I've figured this out non-recursively like this:



noSpaces :: [Char] -> [Char]    
noSpaces xs = [f | r <- xs, f <- if (r == ' ') then "%20" else [r]]


but now i need to figure out how to do that recursively, with no pattern matching and no concatenation...



I figured out other recursive functions (gcd, fib, and a couple others involving numbers) easily enough... but am lost on this for some reason and getting lost-er the more stuff i try that doesn't work...



I found the other answer on here about doing this in Haskell... but that idea uses two separate functions, one being like a helper function -- how would you go about this all in one function, as concisely as possible?



something like:



replace [Char] = [Char]
replace (x:xs)
| x == ' ' = "%20":replace xs


which compiles but of course i get the 'non-exhaustive patterns in function' error message, and can't figure out what to put in the 'otherwise' clause...



any help would be appreciated!










share|improve this question


















  • 1




    If x is a space, it should become "%20". You have that part mostly right (: should be ++). If it’s not a space, it should become what, considering what you have available in the function?
    – Ry-
    Nov 9 at 9:33












  • I imagine you have to do it recursively as an exercise for class. Just for your edification, the way you have done it with a list comprehension is quite nice; a recursive implementation of this function would be awkward "in the wild".
    – luqui
    Nov 9 at 21:24










  • Thank you luqui, I do appreciate that. And yes, this is for a class -- but not for the actual graded assignment, just the "working through the tutorial" part. I just hate leaving something un-figured-out, when it's implied there is, indeed, a solution. Thank you Ry for making me think.
    – Stormy
    Nov 10 at 23:24













up vote
2
down vote

favorite









up vote
2
down vote

favorite











So I've figured this out non-recursively like this:



noSpaces :: [Char] -> [Char]    
noSpaces xs = [f | r <- xs, f <- if (r == ' ') then "%20" else [r]]


but now i need to figure out how to do that recursively, with no pattern matching and no concatenation...



I figured out other recursive functions (gcd, fib, and a couple others involving numbers) easily enough... but am lost on this for some reason and getting lost-er the more stuff i try that doesn't work...



I found the other answer on here about doing this in Haskell... but that idea uses two separate functions, one being like a helper function -- how would you go about this all in one function, as concisely as possible?



something like:



replace [Char] = [Char]
replace (x:xs)
| x == ' ' = "%20":replace xs


which compiles but of course i get the 'non-exhaustive patterns in function' error message, and can't figure out what to put in the 'otherwise' clause...



any help would be appreciated!










share|improve this question













So I've figured this out non-recursively like this:



noSpaces :: [Char] -> [Char]    
noSpaces xs = [f | r <- xs, f <- if (r == ' ') then "%20" else [r]]


but now i need to figure out how to do that recursively, with no pattern matching and no concatenation...



I figured out other recursive functions (gcd, fib, and a couple others involving numbers) easily enough... but am lost on this for some reason and getting lost-er the more stuff i try that doesn't work...



I found the other answer on here about doing this in Haskell... but that idea uses two separate functions, one being like a helper function -- how would you go about this all in one function, as concisely as possible?



something like:



replace [Char] = [Char]
replace (x:xs)
| x == ' ' = "%20":replace xs


which compiles but of course i get the 'non-exhaustive patterns in function' error message, and can't figure out what to put in the 'otherwise' clause...



any help would be appreciated!







haskell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 9 at 7:35









Stormy

673




673








  • 1




    If x is a space, it should become "%20". You have that part mostly right (: should be ++). If it’s not a space, it should become what, considering what you have available in the function?
    – Ry-
    Nov 9 at 9:33












  • I imagine you have to do it recursively as an exercise for class. Just for your edification, the way you have done it with a list comprehension is quite nice; a recursive implementation of this function would be awkward "in the wild".
    – luqui
    Nov 9 at 21:24










  • Thank you luqui, I do appreciate that. And yes, this is for a class -- but not for the actual graded assignment, just the "working through the tutorial" part. I just hate leaving something un-figured-out, when it's implied there is, indeed, a solution. Thank you Ry for making me think.
    – Stormy
    Nov 10 at 23:24














  • 1




    If x is a space, it should become "%20". You have that part mostly right (: should be ++). If it’s not a space, it should become what, considering what you have available in the function?
    – Ry-
    Nov 9 at 9:33












  • I imagine you have to do it recursively as an exercise for class. Just for your edification, the way you have done it with a list comprehension is quite nice; a recursive implementation of this function would be awkward "in the wild".
    – luqui
    Nov 9 at 21:24










  • Thank you luqui, I do appreciate that. And yes, this is for a class -- but not for the actual graded assignment, just the "working through the tutorial" part. I just hate leaving something un-figured-out, when it's implied there is, indeed, a solution. Thank you Ry for making me think.
    – Stormy
    Nov 10 at 23:24








1




1




If x is a space, it should become "%20". You have that part mostly right (: should be ++). If it’s not a space, it should become what, considering what you have available in the function?
– Ry-
Nov 9 at 9:33






If x is a space, it should become "%20". You have that part mostly right (: should be ++). If it’s not a space, it should become what, considering what you have available in the function?
– Ry-
Nov 9 at 9:33














I imagine you have to do it recursively as an exercise for class. Just for your edification, the way you have done it with a list comprehension is quite nice; a recursive implementation of this function would be awkward "in the wild".
– luqui
Nov 9 at 21:24




I imagine you have to do it recursively as an exercise for class. Just for your edification, the way you have done it with a list comprehension is quite nice; a recursive implementation of this function would be awkward "in the wild".
– luqui
Nov 9 at 21:24












Thank you luqui, I do appreciate that. And yes, this is for a class -- but not for the actual graded assignment, just the "working through the tutorial" part. I just hate leaving something un-figured-out, when it's implied there is, indeed, a solution. Thank you Ry for making me think.
– Stormy
Nov 10 at 23:24




Thank you luqui, I do appreciate that. And yes, this is for a class -- but not for the actual graded assignment, just the "working through the tutorial" part. I just hate leaving something un-figured-out, when it's implied there is, indeed, a solution. Thank you Ry for making me think.
– Stormy
Nov 10 at 23:24












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










You miss the case where it is not a ' '.
So, try this:



replace  = 
replace (x:xs)
| x == ' ' = "%20" ++ replace xs
| otherwise = x : replace xs


You need to use ++ in your first guard because a string is a list of chars.






share|improve this answer

















  • 1




    ... | x==' ' = '%':'2':'0':replace xs also works, avoiding ++, but it's less readable
    – chi
    Nov 9 at 12:21










  • @chi yeah, you’re right! I think they will understand the idea this way.
    – zediogoviana
    Nov 9 at 12:30










  • I do like the "more readable" especially when I'm just learning the language... I've only been at this for a week now. Thank you.
    – Stormy
    Nov 10 at 23:25










  • @Stormy you're welcome! Hope this solved your problem.
    – zediogoviana
    Nov 10 at 23:31


















up vote
2
down vote













Try this



replace      = 
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs





share|improve this answer























  • missing 'if' keyword...
    – assembly.jc
    Nov 9 at 8:50











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221513%2fhow-would-i-replace-spaces-in-a-string-with-20-recursively%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










You miss the case where it is not a ' '.
So, try this:



replace  = 
replace (x:xs)
| x == ' ' = "%20" ++ replace xs
| otherwise = x : replace xs


You need to use ++ in your first guard because a string is a list of chars.






share|improve this answer

















  • 1




    ... | x==' ' = '%':'2':'0':replace xs also works, avoiding ++, but it's less readable
    – chi
    Nov 9 at 12:21










  • @chi yeah, you’re right! I think they will understand the idea this way.
    – zediogoviana
    Nov 9 at 12:30










  • I do like the "more readable" especially when I'm just learning the language... I've only been at this for a week now. Thank you.
    – Stormy
    Nov 10 at 23:25










  • @Stormy you're welcome! Hope this solved your problem.
    – zediogoviana
    Nov 10 at 23:31















up vote
3
down vote



accepted










You miss the case where it is not a ' '.
So, try this:



replace  = 
replace (x:xs)
| x == ' ' = "%20" ++ replace xs
| otherwise = x : replace xs


You need to use ++ in your first guard because a string is a list of chars.






share|improve this answer

















  • 1




    ... | x==' ' = '%':'2':'0':replace xs also works, avoiding ++, but it's less readable
    – chi
    Nov 9 at 12:21










  • @chi yeah, you’re right! I think they will understand the idea this way.
    – zediogoviana
    Nov 9 at 12:30










  • I do like the "more readable" especially when I'm just learning the language... I've only been at this for a week now. Thank you.
    – Stormy
    Nov 10 at 23:25










  • @Stormy you're welcome! Hope this solved your problem.
    – zediogoviana
    Nov 10 at 23:31













up vote
3
down vote



accepted







up vote
3
down vote



accepted






You miss the case where it is not a ' '.
So, try this:



replace  = 
replace (x:xs)
| x == ' ' = "%20" ++ replace xs
| otherwise = x : replace xs


You need to use ++ in your first guard because a string is a list of chars.






share|improve this answer












You miss the case where it is not a ' '.
So, try this:



replace  = 
replace (x:xs)
| x == ' ' = "%20" ++ replace xs
| otherwise = x : replace xs


You need to use ++ in your first guard because a string is a list of chars.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 11:29









zediogoviana

1638




1638








  • 1




    ... | x==' ' = '%':'2':'0':replace xs also works, avoiding ++, but it's less readable
    – chi
    Nov 9 at 12:21










  • @chi yeah, you’re right! I think they will understand the idea this way.
    – zediogoviana
    Nov 9 at 12:30










  • I do like the "more readable" especially when I'm just learning the language... I've only been at this for a week now. Thank you.
    – Stormy
    Nov 10 at 23:25










  • @Stormy you're welcome! Hope this solved your problem.
    – zediogoviana
    Nov 10 at 23:31














  • 1




    ... | x==' ' = '%':'2':'0':replace xs also works, avoiding ++, but it's less readable
    – chi
    Nov 9 at 12:21










  • @chi yeah, you’re right! I think they will understand the idea this way.
    – zediogoviana
    Nov 9 at 12:30










  • I do like the "more readable" especially when I'm just learning the language... I've only been at this for a week now. Thank you.
    – Stormy
    Nov 10 at 23:25










  • @Stormy you're welcome! Hope this solved your problem.
    – zediogoviana
    Nov 10 at 23:31








1




1




... | x==' ' = '%':'2':'0':replace xs also works, avoiding ++, but it's less readable
– chi
Nov 9 at 12:21




... | x==' ' = '%':'2':'0':replace xs also works, avoiding ++, but it's less readable
– chi
Nov 9 at 12:21












@chi yeah, you’re right! I think they will understand the idea this way.
– zediogoviana
Nov 9 at 12:30




@chi yeah, you’re right! I think they will understand the idea this way.
– zediogoviana
Nov 9 at 12:30












I do like the "more readable" especially when I'm just learning the language... I've only been at this for a week now. Thank you.
– Stormy
Nov 10 at 23:25




I do like the "more readable" especially when I'm just learning the language... I've only been at this for a week now. Thank you.
– Stormy
Nov 10 at 23:25












@Stormy you're welcome! Hope this solved your problem.
– zediogoviana
Nov 10 at 23:31




@Stormy you're welcome! Hope this solved your problem.
– zediogoviana
Nov 10 at 23:31












up vote
2
down vote













Try this



replace      = 
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs





share|improve this answer























  • missing 'if' keyword...
    – assembly.jc
    Nov 9 at 8:50















up vote
2
down vote













Try this



replace      = 
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs





share|improve this answer























  • missing 'if' keyword...
    – assembly.jc
    Nov 9 at 8:50













up vote
2
down vote










up vote
2
down vote









Try this



replace      = 
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs





share|improve this answer














Try this



replace      = 
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 9 at 9:05









AJFarmar

8,41922652




8,41922652










answered Nov 9 at 7:40









talex

7,9711545




7,9711545












  • missing 'if' keyword...
    – assembly.jc
    Nov 9 at 8:50


















  • missing 'if' keyword...
    – assembly.jc
    Nov 9 at 8:50
















missing 'if' keyword...
– assembly.jc
Nov 9 at 8:50




missing 'if' keyword...
– assembly.jc
Nov 9 at 8:50


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221513%2fhow-would-i-replace-spaces-in-a-string-with-20-recursively%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff