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!
haskell
add a comment |
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!
haskell
1
Ifx
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
add a comment |
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!
haskell
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
haskell
asked Nov 9 at 7:35
Stormy
673
673
1
Ifx
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
add a comment |
1
Ifx
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
add a comment |
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.
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
add a comment |
up vote
2
down vote
Try this
replace =
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs
missing 'if' keyword...
– assembly.jc
Nov 9 at 8:50
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
up vote
2
down vote
Try this
replace =
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs
missing 'if' keyword...
– assembly.jc
Nov 9 at 8:50
add a comment |
up vote
2
down vote
Try this
replace =
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs
missing 'if' keyword...
– assembly.jc
Nov 9 at 8:50
add a comment |
up vote
2
down vote
up vote
2
down vote
Try this
replace =
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs
Try this
replace =
replace (x:xs) = (if x == ' ' then "%20" else [x]) ++ replace xs
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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