Splitting a word in all possible “Subwords”-All possible combinations W/O imports [duplicate]All possible sublists of a listSplitting a word into all possible 'subwords' - All possible combinationsHow to combinate characters in a string once?How to merge a txt file as a [string] and a output [string] to 1 string in Haskell?Generating all the combinations of a set of boolean variables in HaskellHow to search all words from input string in ios?Modifying and Adding to a list in HaskellNeed explanation of Pseudocode and code - Python 3+List of all possible combinations with unique element from each list without duplicate elementsPrint out all possible strings in C using recursionGet all combinations without duplicatesBoggle program not finding all possible words
Why does Kathryn say this in 12 Monkeys?
Is this half mask suitable for spray painting?
Why doesn’t a normal window produce an apparent rainbow?
How hard would it be to convert a glider into an powered electric aircraft?
Are "living" organ banks practical?
Cause of continuous spectral lines
What is the purpose of building foundations?
What's up with this leaf?
Etymology of 'calcit(r)are'?
Implement Homestuck's Catenative Doomsday Dice Cascader
siunitx error: Invalid numerical input
Strange symbol for two functions
How many times can you cast a card exiled by Release to the Wind?
How can non-coders use programs on Github?
From the list of 3-tuples, how can I select tuples which contain one for more nines?
Translating 'Liber'
Trapping Rain Water
Notation of last measure of a song with a pickup measure
Where does this pattern of naming products come from?
How Can I Tell The Difference Between Unmarked Sugar and Stevia?
What is this solid state starting relay component?
Building a road to escape Earth's gravity by making a pyramid on Antartica
2.8 is missing the Carve option in the Boolean Modifier
How is it possible that Gollum speaks Westron?
Splitting a word in all possible “Subwords”-All possible combinations W/O imports [duplicate]
All possible sublists of a listSplitting a word into all possible 'subwords' - All possible combinationsHow to combinate characters in a string once?How to merge a txt file as a [string] and a output [string] to 1 string in Haskell?Generating all the combinations of a set of boolean variables in HaskellHow to search all words from input string in ios?Modifying and Adding to a list in HaskellNeed explanation of Pseudocode and code - Python 3+List of all possible combinations with unique element from each list without duplicate elementsPrint out all possible strings in C using recursionGet all combinations without duplicatesBoggle program not finding all possible words
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
All possible sublists of a list
1 answer
I am trying to get all the possible combinations of a word in the following manner WITHOUT using any imports:
For example...
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
This problem has been bothering me for some time and i can't seem to figure out an algorithm to do this..
The code below is what i've done but this gives all the possible combinations of a string but not in the manner that i need.
I tried to implement this python code in haskell but i wasn't able to accomplish it. It basically is the same problem, but you don't have loops in haskell.
Splitting a word into all possible 'subwords' - All possible combinations
The output of the code below is...
["sun","su","s","un","u","n"]
and not
[["s","un"],["s","u","n"],["su","n"]]
-----------------------------------------------------
substring :: String -> [String]
substring [] = []
substring xs = subs xs ++ substring (tail xs)
where
subs xs = foldl step [] xs
step [] a = [[a]]
step acc a = (head acc ++ [a]) : acc
---------------EXAMPLES OF EXPECTED RESULTS BELOW----------------------------------
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
Input: Sun
Output: [["s","un"],["s","u","n"],["su","n"]]
haskell recursion haskell-platform
marked as duplicate by dfeuer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 25 at 3:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
All possible sublists of a list
1 answer
I am trying to get all the possible combinations of a word in the following manner WITHOUT using any imports:
For example...
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
This problem has been bothering me for some time and i can't seem to figure out an algorithm to do this..
The code below is what i've done but this gives all the possible combinations of a string but not in the manner that i need.
I tried to implement this python code in haskell but i wasn't able to accomplish it. It basically is the same problem, but you don't have loops in haskell.
Splitting a word into all possible 'subwords' - All possible combinations
The output of the code below is...
["sun","su","s","un","u","n"]
and not
[["s","un"],["s","u","n"],["su","n"]]
-----------------------------------------------------
substring :: String -> [String]
substring [] = []
substring xs = subs xs ++ substring (tail xs)
where
subs xs = foldl step [] xs
step [] a = [[a]]
step acc a = (head acc ++ [a]) : acc
---------------EXAMPLES OF EXPECTED RESULTS BELOW----------------------------------
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
Input: Sun
Output: [["s","un"],["s","u","n"],["su","n"]]
haskell recursion haskell-platform
marked as duplicate by dfeuer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 25 at 3:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
All possible sublists of a list
1 answer
I am trying to get all the possible combinations of a word in the following manner WITHOUT using any imports:
For example...
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
This problem has been bothering me for some time and i can't seem to figure out an algorithm to do this..
The code below is what i've done but this gives all the possible combinations of a string but not in the manner that i need.
I tried to implement this python code in haskell but i wasn't able to accomplish it. It basically is the same problem, but you don't have loops in haskell.
Splitting a word into all possible 'subwords' - All possible combinations
The output of the code below is...
["sun","su","s","un","u","n"]
and not
[["s","un"],["s","u","n"],["su","n"]]
-----------------------------------------------------
substring :: String -> [String]
substring [] = []
substring xs = subs xs ++ substring (tail xs)
where
subs xs = foldl step [] xs
step [] a = [[a]]
step acc a = (head acc ++ [a]) : acc
---------------EXAMPLES OF EXPECTED RESULTS BELOW----------------------------------
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
Input: Sun
Output: [["s","un"],["s","u","n"],["su","n"]]
haskell recursion haskell-platform
This question already has an answer here:
All possible sublists of a list
1 answer
I am trying to get all the possible combinations of a word in the following manner WITHOUT using any imports:
For example...
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
This problem has been bothering me for some time and i can't seem to figure out an algorithm to do this..
The code below is what i've done but this gives all the possible combinations of a string but not in the manner that i need.
I tried to implement this python code in haskell but i wasn't able to accomplish it. It basically is the same problem, but you don't have loops in haskell.
Splitting a word into all possible 'subwords' - All possible combinations
The output of the code below is...
["sun","su","s","un","u","n"]
and not
[["s","un"],["s","u","n"],["su","n"]]
-----------------------------------------------------
substring :: String -> [String]
substring [] = []
substring xs = subs xs ++ substring (tail xs)
where
subs xs = foldl step [] xs
step [] a = [[a]]
step acc a = (head acc ++ [a]) : acc
---------------EXAMPLES OF EXPECTED RESULTS BELOW----------------------------------
Input: Bang
Output: [['B','ang'], ['Ba','ng'], ['Ban','g'], ['B','a','ng'], ['B','an','g'], ['Ba','n','g'], ['B','a','n','g']]
Input: Sun
Output: [["s","un"],["s","u","n"],["su","n"]]
This question already has an answer here:
All possible sublists of a list
1 answer
haskell recursion haskell-platform
haskell recursion haskell-platform
asked Mar 24 at 15:29
RottenCrusaderRottenCrusader
163
163
marked as duplicate by dfeuer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 25 at 3:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by dfeuer
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Mar 25 at 3:50
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Recursion is here to help. Say we have a non-empty list x : xs
. We want to know subString (x : xs)
. We apply our solution recursively to xs
, so subString xs
is a list of all solutions for xs
. However we still have that single x
. There are exactly two ways to bring back x
in the solution for x : xs
which covers for the entire solution set of subString (x : xs)
:
- Bring
x
back without attaching it to its neighbor. If we havex : xs = "Bang"
thenx
will be'B'
andxs
will be"ang"
andsubString "ang"
will be[["ang"],["an","g"],["a","ng"],["a","n","g"]]
. This is done by[[x] : u | u <- subString xs]
. Hereu
is a list of Strings, for example["a","ng"]
. Asx
is a character we must turn it to a String, this is done by[x]
, attaching it to the head of the list goes by[x] : u
, so["B","a","ng"]
. The list comprehension will do it for all elements insubString xs
. - Bring
x
back attaching it to its neighbors. An arbitrary solution ofsubString xs
will look likeu : us
. We want to attachx
to the first element ofu : us
which isu
. Sox : u
. For exampleu : us = ["a","n","g"]
sou
will be"a"
andus
will be["n","g"]
. Attaching'B'
to"a"
is done by'B' : "a"
and will give"Ba"
. We have to put"Ba
back in the list so(x : u) : us
. The list comp[rehension looks like[(x : u) : us | (u : us) <- subString xs]
.
We are still left with the case of a String of a single character. We write [x]
for that where x
is the single character. So subString [x]
will be [[[x]]]
.
We have to join the solutions together so
subString :: String -> [[String]]
subString [x] = [[[x]]]
subString (x : xs) = [(x : u) : us | (u : us) <- subString xs] ++ [[x] : u | u <- subString xs]
Example
*Main> subString "Bang"
[["Bang"],["Ban","g"],["Ba","ng"],["Ba","n","g"],["B","ang"],["B","an","g"],["B","a","ng"],["B","a","n","g"]]
1
Note thatsubString' ""
will crash your entire program with a non-exhaustive patterns error.
– Joseph Sible
Mar 24 at 17:12
@JosephSible True but easy to fix if he wants.
– Elmex80s
Mar 24 at 17:13
Thanks for the quick answer!
– RottenCrusader
Mar 24 at 17:20
@Elmex80s could you furtherly elaborate how the code works?I am having problems understanding it because i am new to haskell. I am assuming that it is something like the answer below given by JosephSible but im having troubles understanding its logic.Thanks again in advance!
– RottenCrusader
Mar 24 at 17:41
here it is for you
– Elmex80s
Mar 24 at 23:00
add a comment |
Note that the type signature of your attempt is wrong. You want all of the combinations of subword splits, which is a list of list of strings, but your type is just a list of list of strings.
This will work:
onHead :: (a -> a) -> [a] -> [a]
onHead _ [] = []
onHead f (x:xs) = f x:xs
combos :: [a] -> [[[a]]]
combos [] = [[]]
combos [x] = [[[x]]]
combos (x:xs) = [([x]:), onHead (x:)] <*> combos xs
onHead
should be self-explanatory: perform the given function on the head of a list. combos
recurses as follows: the subwords of a string are the subwords of its tail, with two possibilities for each: either the head is its own subword, or it's tacked onto the beginning of the first subword.
Update: Here's another (IMO cleaner) approach:
combos :: Foldable t => t a -> [[[a]]]
combos = foldr (concatMap . go) [[]]
where go x l = ([x]:l):case l of
[] -> []
h:t -> [(x:h):t]
It's using the same technique as above, just with a cleaner implementation.
Thanks! Works like a charm but i accepted the 1st answer just for the sake of being the 1st.Thanks alot though really!
– RottenCrusader
Mar 24 at 17:20
@rempakos You can give +1 to answers if you think it is worth it, even if you do not accept it as the answer.
– Elmex80s
Mar 24 at 17:22
@Elmex80s You need 15 rep to upvote, which rempakos doesn't have yet.
– Joseph Sible
Mar 24 at 17:23
I tried upvoting but it didn't let me because i don't have the rep, but ill visit back and upvote if i reach that number!Thanks again!
– RottenCrusader
Mar 24 at 17:25
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Recursion is here to help. Say we have a non-empty list x : xs
. We want to know subString (x : xs)
. We apply our solution recursively to xs
, so subString xs
is a list of all solutions for xs
. However we still have that single x
. There are exactly two ways to bring back x
in the solution for x : xs
which covers for the entire solution set of subString (x : xs)
:
- Bring
x
back without attaching it to its neighbor. If we havex : xs = "Bang"
thenx
will be'B'
andxs
will be"ang"
andsubString "ang"
will be[["ang"],["an","g"],["a","ng"],["a","n","g"]]
. This is done by[[x] : u | u <- subString xs]
. Hereu
is a list of Strings, for example["a","ng"]
. Asx
is a character we must turn it to a String, this is done by[x]
, attaching it to the head of the list goes by[x] : u
, so["B","a","ng"]
. The list comprehension will do it for all elements insubString xs
. - Bring
x
back attaching it to its neighbors. An arbitrary solution ofsubString xs
will look likeu : us
. We want to attachx
to the first element ofu : us
which isu
. Sox : u
. For exampleu : us = ["a","n","g"]
sou
will be"a"
andus
will be["n","g"]
. Attaching'B'
to"a"
is done by'B' : "a"
and will give"Ba"
. We have to put"Ba
back in the list so(x : u) : us
. The list comp[rehension looks like[(x : u) : us | (u : us) <- subString xs]
.
We are still left with the case of a String of a single character. We write [x]
for that where x
is the single character. So subString [x]
will be [[[x]]]
.
We have to join the solutions together so
subString :: String -> [[String]]
subString [x] = [[[x]]]
subString (x : xs) = [(x : u) : us | (u : us) <- subString xs] ++ [[x] : u | u <- subString xs]
Example
*Main> subString "Bang"
[["Bang"],["Ban","g"],["Ba","ng"],["Ba","n","g"],["B","ang"],["B","an","g"],["B","a","ng"],["B","a","n","g"]]
1
Note thatsubString' ""
will crash your entire program with a non-exhaustive patterns error.
– Joseph Sible
Mar 24 at 17:12
@JosephSible True but easy to fix if he wants.
– Elmex80s
Mar 24 at 17:13
Thanks for the quick answer!
– RottenCrusader
Mar 24 at 17:20
@Elmex80s could you furtherly elaborate how the code works?I am having problems understanding it because i am new to haskell. I am assuming that it is something like the answer below given by JosephSible but im having troubles understanding its logic.Thanks again in advance!
– RottenCrusader
Mar 24 at 17:41
here it is for you
– Elmex80s
Mar 24 at 23:00
add a comment |
Recursion is here to help. Say we have a non-empty list x : xs
. We want to know subString (x : xs)
. We apply our solution recursively to xs
, so subString xs
is a list of all solutions for xs
. However we still have that single x
. There are exactly two ways to bring back x
in the solution for x : xs
which covers for the entire solution set of subString (x : xs)
:
- Bring
x
back without attaching it to its neighbor. If we havex : xs = "Bang"
thenx
will be'B'
andxs
will be"ang"
andsubString "ang"
will be[["ang"],["an","g"],["a","ng"],["a","n","g"]]
. This is done by[[x] : u | u <- subString xs]
. Hereu
is a list of Strings, for example["a","ng"]
. Asx
is a character we must turn it to a String, this is done by[x]
, attaching it to the head of the list goes by[x] : u
, so["B","a","ng"]
. The list comprehension will do it for all elements insubString xs
. - Bring
x
back attaching it to its neighbors. An arbitrary solution ofsubString xs
will look likeu : us
. We want to attachx
to the first element ofu : us
which isu
. Sox : u
. For exampleu : us = ["a","n","g"]
sou
will be"a"
andus
will be["n","g"]
. Attaching'B'
to"a"
is done by'B' : "a"
and will give"Ba"
. We have to put"Ba
back in the list so(x : u) : us
. The list comp[rehension looks like[(x : u) : us | (u : us) <- subString xs]
.
We are still left with the case of a String of a single character. We write [x]
for that where x
is the single character. So subString [x]
will be [[[x]]]
.
We have to join the solutions together so
subString :: String -> [[String]]
subString [x] = [[[x]]]
subString (x : xs) = [(x : u) : us | (u : us) <- subString xs] ++ [[x] : u | u <- subString xs]
Example
*Main> subString "Bang"
[["Bang"],["Ban","g"],["Ba","ng"],["Ba","n","g"],["B","ang"],["B","an","g"],["B","a","ng"],["B","a","n","g"]]
1
Note thatsubString' ""
will crash your entire program with a non-exhaustive patterns error.
– Joseph Sible
Mar 24 at 17:12
@JosephSible True but easy to fix if he wants.
– Elmex80s
Mar 24 at 17:13
Thanks for the quick answer!
– RottenCrusader
Mar 24 at 17:20
@Elmex80s could you furtherly elaborate how the code works?I am having problems understanding it because i am new to haskell. I am assuming that it is something like the answer below given by JosephSible but im having troubles understanding its logic.Thanks again in advance!
– RottenCrusader
Mar 24 at 17:41
here it is for you
– Elmex80s
Mar 24 at 23:00
add a comment |
Recursion is here to help. Say we have a non-empty list x : xs
. We want to know subString (x : xs)
. We apply our solution recursively to xs
, so subString xs
is a list of all solutions for xs
. However we still have that single x
. There are exactly two ways to bring back x
in the solution for x : xs
which covers for the entire solution set of subString (x : xs)
:
- Bring
x
back without attaching it to its neighbor. If we havex : xs = "Bang"
thenx
will be'B'
andxs
will be"ang"
andsubString "ang"
will be[["ang"],["an","g"],["a","ng"],["a","n","g"]]
. This is done by[[x] : u | u <- subString xs]
. Hereu
is a list of Strings, for example["a","ng"]
. Asx
is a character we must turn it to a String, this is done by[x]
, attaching it to the head of the list goes by[x] : u
, so["B","a","ng"]
. The list comprehension will do it for all elements insubString xs
. - Bring
x
back attaching it to its neighbors. An arbitrary solution ofsubString xs
will look likeu : us
. We want to attachx
to the first element ofu : us
which isu
. Sox : u
. For exampleu : us = ["a","n","g"]
sou
will be"a"
andus
will be["n","g"]
. Attaching'B'
to"a"
is done by'B' : "a"
and will give"Ba"
. We have to put"Ba
back in the list so(x : u) : us
. The list comp[rehension looks like[(x : u) : us | (u : us) <- subString xs]
.
We are still left with the case of a String of a single character. We write [x]
for that where x
is the single character. So subString [x]
will be [[[x]]]
.
We have to join the solutions together so
subString :: String -> [[String]]
subString [x] = [[[x]]]
subString (x : xs) = [(x : u) : us | (u : us) <- subString xs] ++ [[x] : u | u <- subString xs]
Example
*Main> subString "Bang"
[["Bang"],["Ban","g"],["Ba","ng"],["Ba","n","g"],["B","ang"],["B","an","g"],["B","a","ng"],["B","a","n","g"]]
Recursion is here to help. Say we have a non-empty list x : xs
. We want to know subString (x : xs)
. We apply our solution recursively to xs
, so subString xs
is a list of all solutions for xs
. However we still have that single x
. There are exactly two ways to bring back x
in the solution for x : xs
which covers for the entire solution set of subString (x : xs)
:
- Bring
x
back without attaching it to its neighbor. If we havex : xs = "Bang"
thenx
will be'B'
andxs
will be"ang"
andsubString "ang"
will be[["ang"],["an","g"],["a","ng"],["a","n","g"]]
. This is done by[[x] : u | u <- subString xs]
. Hereu
is a list of Strings, for example["a","ng"]
. Asx
is a character we must turn it to a String, this is done by[x]
, attaching it to the head of the list goes by[x] : u
, so["B","a","ng"]
. The list comprehension will do it for all elements insubString xs
. - Bring
x
back attaching it to its neighbors. An arbitrary solution ofsubString xs
will look likeu : us
. We want to attachx
to the first element ofu : us
which isu
. Sox : u
. For exampleu : us = ["a","n","g"]
sou
will be"a"
andus
will be["n","g"]
. Attaching'B'
to"a"
is done by'B' : "a"
and will give"Ba"
. We have to put"Ba
back in the list so(x : u) : us
. The list comp[rehension looks like[(x : u) : us | (u : us) <- subString xs]
.
We are still left with the case of a String of a single character. We write [x]
for that where x
is the single character. So subString [x]
will be [[[x]]]
.
We have to join the solutions together so
subString :: String -> [[String]]
subString [x] = [[[x]]]
subString (x : xs) = [(x : u) : us | (u : us) <- subString xs] ++ [[x] : u | u <- subString xs]
Example
*Main> subString "Bang"
[["Bang"],["Ban","g"],["Ba","ng"],["Ba","n","g"],["B","ang"],["B","an","g"],["B","a","ng"],["B","a","n","g"]]
edited Mar 24 at 22:59
answered Mar 24 at 17:09
Elmex80sElmex80s
2,3551816
2,3551816
1
Note thatsubString' ""
will crash your entire program with a non-exhaustive patterns error.
– Joseph Sible
Mar 24 at 17:12
@JosephSible True but easy to fix if he wants.
– Elmex80s
Mar 24 at 17:13
Thanks for the quick answer!
– RottenCrusader
Mar 24 at 17:20
@Elmex80s could you furtherly elaborate how the code works?I am having problems understanding it because i am new to haskell. I am assuming that it is something like the answer below given by JosephSible but im having troubles understanding its logic.Thanks again in advance!
– RottenCrusader
Mar 24 at 17:41
here it is for you
– Elmex80s
Mar 24 at 23:00
add a comment |
1
Note thatsubString' ""
will crash your entire program with a non-exhaustive patterns error.
– Joseph Sible
Mar 24 at 17:12
@JosephSible True but easy to fix if he wants.
– Elmex80s
Mar 24 at 17:13
Thanks for the quick answer!
– RottenCrusader
Mar 24 at 17:20
@Elmex80s could you furtherly elaborate how the code works?I am having problems understanding it because i am new to haskell. I am assuming that it is something like the answer below given by JosephSible but im having troubles understanding its logic.Thanks again in advance!
– RottenCrusader
Mar 24 at 17:41
here it is for you
– Elmex80s
Mar 24 at 23:00
1
1
Note that
subString' ""
will crash your entire program with a non-exhaustive patterns error.– Joseph Sible
Mar 24 at 17:12
Note that
subString' ""
will crash your entire program with a non-exhaustive patterns error.– Joseph Sible
Mar 24 at 17:12
@JosephSible True but easy to fix if he wants.
– Elmex80s
Mar 24 at 17:13
@JosephSible True but easy to fix if he wants.
– Elmex80s
Mar 24 at 17:13
Thanks for the quick answer!
– RottenCrusader
Mar 24 at 17:20
Thanks for the quick answer!
– RottenCrusader
Mar 24 at 17:20
@Elmex80s could you furtherly elaborate how the code works?I am having problems understanding it because i am new to haskell. I am assuming that it is something like the answer below given by JosephSible but im having troubles understanding its logic.Thanks again in advance!
– RottenCrusader
Mar 24 at 17:41
@Elmex80s could you furtherly elaborate how the code works?I am having problems understanding it because i am new to haskell. I am assuming that it is something like the answer below given by JosephSible but im having troubles understanding its logic.Thanks again in advance!
– RottenCrusader
Mar 24 at 17:41
here it is for you
– Elmex80s
Mar 24 at 23:00
here it is for you
– Elmex80s
Mar 24 at 23:00
add a comment |
Note that the type signature of your attempt is wrong. You want all of the combinations of subword splits, which is a list of list of strings, but your type is just a list of list of strings.
This will work:
onHead :: (a -> a) -> [a] -> [a]
onHead _ [] = []
onHead f (x:xs) = f x:xs
combos :: [a] -> [[[a]]]
combos [] = [[]]
combos [x] = [[[x]]]
combos (x:xs) = [([x]:), onHead (x:)] <*> combos xs
onHead
should be self-explanatory: perform the given function on the head of a list. combos
recurses as follows: the subwords of a string are the subwords of its tail, with two possibilities for each: either the head is its own subword, or it's tacked onto the beginning of the first subword.
Update: Here's another (IMO cleaner) approach:
combos :: Foldable t => t a -> [[[a]]]
combos = foldr (concatMap . go) [[]]
where go x l = ([x]:l):case l of
[] -> []
h:t -> [(x:h):t]
It's using the same technique as above, just with a cleaner implementation.
Thanks! Works like a charm but i accepted the 1st answer just for the sake of being the 1st.Thanks alot though really!
– RottenCrusader
Mar 24 at 17:20
@rempakos You can give +1 to answers if you think it is worth it, even if you do not accept it as the answer.
– Elmex80s
Mar 24 at 17:22
@Elmex80s You need 15 rep to upvote, which rempakos doesn't have yet.
– Joseph Sible
Mar 24 at 17:23
I tried upvoting but it didn't let me because i don't have the rep, but ill visit back and upvote if i reach that number!Thanks again!
– RottenCrusader
Mar 24 at 17:25
add a comment |
Note that the type signature of your attempt is wrong. You want all of the combinations of subword splits, which is a list of list of strings, but your type is just a list of list of strings.
This will work:
onHead :: (a -> a) -> [a] -> [a]
onHead _ [] = []
onHead f (x:xs) = f x:xs
combos :: [a] -> [[[a]]]
combos [] = [[]]
combos [x] = [[[x]]]
combos (x:xs) = [([x]:), onHead (x:)] <*> combos xs
onHead
should be self-explanatory: perform the given function on the head of a list. combos
recurses as follows: the subwords of a string are the subwords of its tail, with two possibilities for each: either the head is its own subword, or it's tacked onto the beginning of the first subword.
Update: Here's another (IMO cleaner) approach:
combos :: Foldable t => t a -> [[[a]]]
combos = foldr (concatMap . go) [[]]
where go x l = ([x]:l):case l of
[] -> []
h:t -> [(x:h):t]
It's using the same technique as above, just with a cleaner implementation.
Thanks! Works like a charm but i accepted the 1st answer just for the sake of being the 1st.Thanks alot though really!
– RottenCrusader
Mar 24 at 17:20
@rempakos You can give +1 to answers if you think it is worth it, even if you do not accept it as the answer.
– Elmex80s
Mar 24 at 17:22
@Elmex80s You need 15 rep to upvote, which rempakos doesn't have yet.
– Joseph Sible
Mar 24 at 17:23
I tried upvoting but it didn't let me because i don't have the rep, but ill visit back and upvote if i reach that number!Thanks again!
– RottenCrusader
Mar 24 at 17:25
add a comment |
Note that the type signature of your attempt is wrong. You want all of the combinations of subword splits, which is a list of list of strings, but your type is just a list of list of strings.
This will work:
onHead :: (a -> a) -> [a] -> [a]
onHead _ [] = []
onHead f (x:xs) = f x:xs
combos :: [a] -> [[[a]]]
combos [] = [[]]
combos [x] = [[[x]]]
combos (x:xs) = [([x]:), onHead (x:)] <*> combos xs
onHead
should be self-explanatory: perform the given function on the head of a list. combos
recurses as follows: the subwords of a string are the subwords of its tail, with two possibilities for each: either the head is its own subword, or it's tacked onto the beginning of the first subword.
Update: Here's another (IMO cleaner) approach:
combos :: Foldable t => t a -> [[[a]]]
combos = foldr (concatMap . go) [[]]
where go x l = ([x]:l):case l of
[] -> []
h:t -> [(x:h):t]
It's using the same technique as above, just with a cleaner implementation.
Note that the type signature of your attempt is wrong. You want all of the combinations of subword splits, which is a list of list of strings, but your type is just a list of list of strings.
This will work:
onHead :: (a -> a) -> [a] -> [a]
onHead _ [] = []
onHead f (x:xs) = f x:xs
combos :: [a] -> [[[a]]]
combos [] = [[]]
combos [x] = [[[x]]]
combos (x:xs) = [([x]:), onHead (x:)] <*> combos xs
onHead
should be self-explanatory: perform the given function on the head of a list. combos
recurses as follows: the subwords of a string are the subwords of its tail, with two possibilities for each: either the head is its own subword, or it's tacked onto the beginning of the first subword.
Update: Here's another (IMO cleaner) approach:
combos :: Foldable t => t a -> [[[a]]]
combos = foldr (concatMap . go) [[]]
where go x l = ([x]:l):case l of
[] -> []
h:t -> [(x:h):t]
It's using the same technique as above, just with a cleaner implementation.
edited May 14 at 0:13
answered Mar 24 at 17:09
Joseph SibleJoseph Sible
9,09031543
9,09031543
Thanks! Works like a charm but i accepted the 1st answer just for the sake of being the 1st.Thanks alot though really!
– RottenCrusader
Mar 24 at 17:20
@rempakos You can give +1 to answers if you think it is worth it, even if you do not accept it as the answer.
– Elmex80s
Mar 24 at 17:22
@Elmex80s You need 15 rep to upvote, which rempakos doesn't have yet.
– Joseph Sible
Mar 24 at 17:23
I tried upvoting but it didn't let me because i don't have the rep, but ill visit back and upvote if i reach that number!Thanks again!
– RottenCrusader
Mar 24 at 17:25
add a comment |
Thanks! Works like a charm but i accepted the 1st answer just for the sake of being the 1st.Thanks alot though really!
– RottenCrusader
Mar 24 at 17:20
@rempakos You can give +1 to answers if you think it is worth it, even if you do not accept it as the answer.
– Elmex80s
Mar 24 at 17:22
@Elmex80s You need 15 rep to upvote, which rempakos doesn't have yet.
– Joseph Sible
Mar 24 at 17:23
I tried upvoting but it didn't let me because i don't have the rep, but ill visit back and upvote if i reach that number!Thanks again!
– RottenCrusader
Mar 24 at 17:25
Thanks! Works like a charm but i accepted the 1st answer just for the sake of being the 1st.Thanks alot though really!
– RottenCrusader
Mar 24 at 17:20
Thanks! Works like a charm but i accepted the 1st answer just for the sake of being the 1st.Thanks alot though really!
– RottenCrusader
Mar 24 at 17:20
@rempakos You can give +1 to answers if you think it is worth it, even if you do not accept it as the answer.
– Elmex80s
Mar 24 at 17:22
@rempakos You can give +1 to answers if you think it is worth it, even if you do not accept it as the answer.
– Elmex80s
Mar 24 at 17:22
@Elmex80s You need 15 rep to upvote, which rempakos doesn't have yet.
– Joseph Sible
Mar 24 at 17:23
@Elmex80s You need 15 rep to upvote, which rempakos doesn't have yet.
– Joseph Sible
Mar 24 at 17:23
I tried upvoting but it didn't let me because i don't have the rep, but ill visit back and upvote if i reach that number!Thanks again!
– RottenCrusader
Mar 24 at 17:25
I tried upvoting but it didn't let me because i don't have the rep, but ill visit back and upvote if i reach that number!Thanks again!
– RottenCrusader
Mar 24 at 17:25
add a comment |