Haskell: How to remove a List from a List in Haskell
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:
> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]
> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"
removeAll _ =
removeAll y = y
removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys
haskell
add a comment |
I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:
> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]
> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"
removeAll _ =
removeAll y = y
removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys
haskell
1
What is your question?
– talex
Nov 22 '18 at 5:43
3
You can always look at the source of the built-in functions, so you shouldn't count those out.
– Mateen Ulhaq
Nov 22 '18 at 5:44
add a comment |
I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:
> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]
> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"
removeAll _ =
removeAll y = y
removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys
haskell
I have a first list and a second list, and I want to write a function that takes two lists and returns a list containing only those elements in the second list that do not appear in the first. And I don't want to use built-in functions. For example:
> removeAll [1..3] [0..10]
[0,4,5,6,7,8,9,10]
> removeAll "aeiou" "supercalifragilisticexpialidocious"
"sprclfrglstcxpldcs"
removeAll _ =
removeAll y = y
removeAll (x:xs) (y:ys)
| x==y = removeAll xs ys
| otherwise = y:removeAll xs ys
haskell
haskell
edited Nov 22 '18 at 5:39
m0nhawk
15.9k83263
15.9k83263
asked Nov 22 '18 at 5:30
zheng zhongzheng zhong
94
94
1
What is your question?
– talex
Nov 22 '18 at 5:43
3
You can always look at the source of the built-in functions, so you shouldn't count those out.
– Mateen Ulhaq
Nov 22 '18 at 5:44
add a comment |
1
What is your question?
– talex
Nov 22 '18 at 5:43
3
You can always look at the source of the built-in functions, so you shouldn't count those out.
– Mateen Ulhaq
Nov 22 '18 at 5:44
1
1
What is your question?
– talex
Nov 22 '18 at 5:43
What is your question?
– talex
Nov 22 '18 at 5:43
3
3
You can always look at the source of the built-in functions, so you shouldn't count those out.
– Mateen Ulhaq
Nov 22 '18 at 5:44
You can always look at the source of the built-in functions, so you shouldn't count those out.
– Mateen Ulhaq
Nov 22 '18 at 5:44
add a comment |
3 Answers
3
active
oldest
votes
There is a pretty straightforward recursive definition
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll toRemove (x:xs)
| x `belongs` toRemove = removeAll toRemove xs
| otherwise = x:removeAll toRemove xs
where belongs _ = False
belongs a (y:ys)
| a == y = True
| otherwise = belongs a ys
The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.
It works, thank you very much!
– zheng zhong
Nov 22 '18 at 14:25
add a comment |
This is one simple way:
removeAll ignores = filter (x -> notElem x ignores)
which makes use of:
filternotElem
If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:
myFilter _ =
myFilter pred (x:xs)
| pred x = x : myFilter pred xs
| otherwise = myFilter pred xs
myNotElem e = True
myNotElem e (x:xs)
| e == x = False
| otherwise = myNotElem e xs
removeAll ignores = myFilter (x -> myNotElem x ignores)
Thank you for your answer!
– zheng zhong
Nov 22 '18 at 14:26
add a comment |
A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll x (y:ys)
| myElem y x = removeAll x ys
| otherwise = y:removeAll x ys
If you do not want to use the built-in elem as well, you can define it similarly:
myElem :: Eq a => a -> [a] -> Bool
myElem _ = False
myElem x (y:ys)
| x == y = True
| otherwise = myElem x ys
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53424422%2fhaskell-how-to-remove-a-list-from-a-list-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is a pretty straightforward recursive definition
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll toRemove (x:xs)
| x `belongs` toRemove = removeAll toRemove xs
| otherwise = x:removeAll toRemove xs
where belongs _ = False
belongs a (y:ys)
| a == y = True
| otherwise = belongs a ys
The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.
It works, thank you very much!
– zheng zhong
Nov 22 '18 at 14:25
add a comment |
There is a pretty straightforward recursive definition
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll toRemove (x:xs)
| x `belongs` toRemove = removeAll toRemove xs
| otherwise = x:removeAll toRemove xs
where belongs _ = False
belongs a (y:ys)
| a == y = True
| otherwise = belongs a ys
The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.
It works, thank you very much!
– zheng zhong
Nov 22 '18 at 14:25
add a comment |
There is a pretty straightforward recursive definition
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll toRemove (x:xs)
| x `belongs` toRemove = removeAll toRemove xs
| otherwise = x:removeAll toRemove xs
where belongs _ = False
belongs a (y:ys)
| a == y = True
| otherwise = belongs a ys
The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.
There is a pretty straightforward recursive definition
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll toRemove (x:xs)
| x `belongs` toRemove = removeAll toRemove xs
| otherwise = x:removeAll toRemove xs
where belongs _ = False
belongs a (y:ys)
| a == y = True
| otherwise = belongs a ys
The belongs function is just the prelude defined elem. Since you don't want to use built-in functions you have to define it yourself.
answered Nov 22 '18 at 7:36
lsmorlsmor
1,004114
1,004114
It works, thank you very much!
– zheng zhong
Nov 22 '18 at 14:25
add a comment |
It works, thank you very much!
– zheng zhong
Nov 22 '18 at 14:25
It works, thank you very much!
– zheng zhong
Nov 22 '18 at 14:25
It works, thank you very much!
– zheng zhong
Nov 22 '18 at 14:25
add a comment |
This is one simple way:
removeAll ignores = filter (x -> notElem x ignores)
which makes use of:
filternotElem
If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:
myFilter _ =
myFilter pred (x:xs)
| pred x = x : myFilter pred xs
| otherwise = myFilter pred xs
myNotElem e = True
myNotElem e (x:xs)
| e == x = False
| otherwise = myNotElem e xs
removeAll ignores = myFilter (x -> myNotElem x ignores)
Thank you for your answer!
– zheng zhong
Nov 22 '18 at 14:26
add a comment |
This is one simple way:
removeAll ignores = filter (x -> notElem x ignores)
which makes use of:
filternotElem
If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:
myFilter _ =
myFilter pred (x:xs)
| pred x = x : myFilter pred xs
| otherwise = myFilter pred xs
myNotElem e = True
myNotElem e (x:xs)
| e == x = False
| otherwise = myNotElem e xs
removeAll ignores = myFilter (x -> myNotElem x ignores)
Thank you for your answer!
– zheng zhong
Nov 22 '18 at 14:26
add a comment |
This is one simple way:
removeAll ignores = filter (x -> notElem x ignores)
which makes use of:
filternotElem
If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:
myFilter _ =
myFilter pred (x:xs)
| pred x = x : myFilter pred xs
| otherwise = myFilter pred xs
myNotElem e = True
myNotElem e (x:xs)
| e == x = False
| otherwise = myNotElem e xs
removeAll ignores = myFilter (x -> myNotElem x ignores)
This is one simple way:
removeAll ignores = filter (x -> notElem x ignores)
which makes use of:
filternotElem
If you wanted to write it in "pure-pure" Haskell, with no auxiliary functions... well, it sounds like it would be pretty ugly since we'd need to do some sort of nested for-loop. Here's a compromise:
myFilter _ =
myFilter pred (x:xs)
| pred x = x : myFilter pred xs
| otherwise = myFilter pred xs
myNotElem e = True
myNotElem e (x:xs)
| e == x = False
| otherwise = myNotElem e xs
removeAll ignores = myFilter (x -> myNotElem x ignores)
edited Nov 22 '18 at 6:16
answered Nov 22 '18 at 6:09
Mateen UlhaqMateen Ulhaq
11.6k114994
11.6k114994
Thank you for your answer!
– zheng zhong
Nov 22 '18 at 14:26
add a comment |
Thank you for your answer!
– zheng zhong
Nov 22 '18 at 14:26
Thank you for your answer!
– zheng zhong
Nov 22 '18 at 14:26
Thank you for your answer!
– zheng zhong
Nov 22 '18 at 14:26
add a comment |
A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll x (y:ys)
| myElem y x = removeAll x ys
| otherwise = y:removeAll x ys
If you do not want to use the built-in elem as well, you can define it similarly:
myElem :: Eq a => a -> [a] -> Bool
myElem _ = False
myElem x (y:ys)
| x == y = True
| otherwise = myElem x ys
add a comment |
A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll x (y:ys)
| myElem y x = removeAll x ys
| otherwise = y:removeAll x ys
If you do not want to use the built-in elem as well, you can define it similarly:
myElem :: Eq a => a -> [a] -> Bool
myElem _ = False
myElem x (y:ys)
| x == y = True
| otherwise = myElem x ys
add a comment |
A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll x (y:ys)
| myElem y x = removeAll x ys
| otherwise = y:removeAll x ys
If you do not want to use the built-in elem as well, you can define it similarly:
myElem :: Eq a => a -> [a] -> Bool
myElem _ = False
myElem x (y:ys)
| x == y = True
| otherwise = myElem x ys
A problem of your current code is that you tried to recurse in two lists simultaneously in one function. You need to divide and conquer. If elem is allowed, you may write:
removeAll :: Eq a => [a] -> [a] -> [a]
removeAll _ =
removeAll x (y:ys)
| myElem y x = removeAll x ys
| otherwise = y:removeAll x ys
If you do not want to use the built-in elem as well, you can define it similarly:
myElem :: Eq a => a -> [a] -> Bool
myElem _ = False
myElem x (y:ys)
| x == y = True
| otherwise = myElem x ys
answered Nov 23 '18 at 8:22
Yongwei WuYongwei Wu
2,1761832
2,1761832
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53424422%2fhaskell-how-to-remove-a-list-from-a-list-in-haskell%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
What is your question?
– talex
Nov 22 '18 at 5:43
3
You can always look at the source of the built-in functions, so you shouldn't count those out.
– Mateen Ulhaq
Nov 22 '18 at 5:44