Map with condition in Haskell
up vote
0
down vote
favorite
I want to make a mapping to an array with a function but only is a condition is fullfilled. For my particular problem I have an array of objects and only want to do a function on a sub-array of this big array.
function :: a -> a
mapping :: [a] -> [a] -> [a]
mapping all sub = map (x -> if (x `elem` sub) then function x else x) all
how can I do something like this ?
edit: I know this code works but in my class this is considered bad design and we need to avoid using if statements and need to use guards and such instead.
haskell
|
show 1 more comment
up vote
0
down vote
favorite
I want to make a mapping to an array with a function but only is a condition is fullfilled. For my particular problem I have an array of objects and only want to do a function on a sub-array of this big array.
function :: a -> a
mapping :: [a] -> [a] -> [a]
mapping all sub = map (x -> if (x `elem` sub) then function x else x) all
how can I do something like this ?
edit: I know this code works but in my class this is considered bad design and we need to avoid using if statements and need to use guards and such instead.
haskell
4
Your example is legal Haskell code. What exactly is your problem? That is, what about the solution you already have doesn’t work?
– Alexis King
Nov 11 at 15:56
2
do you want this: mapping p f xs= map (x-> if p x then f x else x) xs
– assembly.jc
Nov 11 at 16:04
well the code I have now is considered bad design in my class, we need to try to avoid if and use guards and such instead
– Glenn
Nov 11 at 16:10
I'd not consider that bad design at all. Still, if you want to use guards you can usemapping all sub = map f all where f x | x `elem` sub = function x | otherwise = x, giving a name for the function youmap
– chi
Nov 11 at 16:19
4
Note that you are working with lists, and not arrays. This isn't just terminology nitpicking; it is important to keep the difference in mind as you learn to work with Haskell lists.
– duplode
Nov 11 at 16:21
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to make a mapping to an array with a function but only is a condition is fullfilled. For my particular problem I have an array of objects and only want to do a function on a sub-array of this big array.
function :: a -> a
mapping :: [a] -> [a] -> [a]
mapping all sub = map (x -> if (x `elem` sub) then function x else x) all
how can I do something like this ?
edit: I know this code works but in my class this is considered bad design and we need to avoid using if statements and need to use guards and such instead.
haskell
I want to make a mapping to an array with a function but only is a condition is fullfilled. For my particular problem I have an array of objects and only want to do a function on a sub-array of this big array.
function :: a -> a
mapping :: [a] -> [a] -> [a]
mapping all sub = map (x -> if (x `elem` sub) then function x else x) all
how can I do something like this ?
edit: I know this code works but in my class this is considered bad design and we need to avoid using if statements and need to use guards and such instead.
haskell
haskell
edited Nov 11 at 16:13
asked Nov 11 at 15:55
Glenn
113
113
4
Your example is legal Haskell code. What exactly is your problem? That is, what about the solution you already have doesn’t work?
– Alexis King
Nov 11 at 15:56
2
do you want this: mapping p f xs= map (x-> if p x then f x else x) xs
– assembly.jc
Nov 11 at 16:04
well the code I have now is considered bad design in my class, we need to try to avoid if and use guards and such instead
– Glenn
Nov 11 at 16:10
I'd not consider that bad design at all. Still, if you want to use guards you can usemapping all sub = map f all where f x | x `elem` sub = function x | otherwise = x, giving a name for the function youmap
– chi
Nov 11 at 16:19
4
Note that you are working with lists, and not arrays. This isn't just terminology nitpicking; it is important to keep the difference in mind as you learn to work with Haskell lists.
– duplode
Nov 11 at 16:21
|
show 1 more comment
4
Your example is legal Haskell code. What exactly is your problem? That is, what about the solution you already have doesn’t work?
– Alexis King
Nov 11 at 15:56
2
do you want this: mapping p f xs= map (x-> if p x then f x else x) xs
– assembly.jc
Nov 11 at 16:04
well the code I have now is considered bad design in my class, we need to try to avoid if and use guards and such instead
– Glenn
Nov 11 at 16:10
I'd not consider that bad design at all. Still, if you want to use guards you can usemapping all sub = map f all where f x | x `elem` sub = function x | otherwise = x, giving a name for the function youmap
– chi
Nov 11 at 16:19
4
Note that you are working with lists, and not arrays. This isn't just terminology nitpicking; it is important to keep the difference in mind as you learn to work with Haskell lists.
– duplode
Nov 11 at 16:21
4
4
Your example is legal Haskell code. What exactly is your problem? That is, what about the solution you already have doesn’t work?
– Alexis King
Nov 11 at 15:56
Your example is legal Haskell code. What exactly is your problem? That is, what about the solution you already have doesn’t work?
– Alexis King
Nov 11 at 15:56
2
2
do you want this: mapping p f xs= map (x-> if p x then f x else x) xs
– assembly.jc
Nov 11 at 16:04
do you want this: mapping p f xs= map (x-> if p x then f x else x) xs
– assembly.jc
Nov 11 at 16:04
well the code I have now is considered bad design in my class, we need to try to avoid if and use guards and such instead
– Glenn
Nov 11 at 16:10
well the code I have now is considered bad design in my class, we need to try to avoid if and use guards and such instead
– Glenn
Nov 11 at 16:10
I'd not consider that bad design at all. Still, if you want to use guards you can use
mapping all sub = map f all where f x | x `elem` sub = function x | otherwise = x, giving a name for the function you map– chi
Nov 11 at 16:19
I'd not consider that bad design at all. Still, if you want to use guards you can use
mapping all sub = map f all where f x | x `elem` sub = function x | otherwise = x, giving a name for the function you map– chi
Nov 11 at 16:19
4
4
Note that you are working with lists, and not arrays. This isn't just terminology nitpicking; it is important to keep the difference in mind as you learn to work with Haskell lists.
– duplode
Nov 11 at 16:21
Note that you are working with lists, and not arrays. This isn't just terminology nitpicking; it is important to keep the difference in mind as you learn to work with Haskell lists.
– duplode
Nov 11 at 16:21
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
5
down vote
The part that seems like bad design to me is that you fail to factor out the basic pattern. As assembly.jc has pointed out, you should really give that its own function.
mapOnly :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapOnly p f = map $ x -> if p x then f x else x
Using if seems quite reasonable style in this context; anything else really comes off as clunky.
add a comment |
up vote
0
down vote
If you are force to use guards, is pretty straightforward. I think if .. then .. else is a good pattern here though. As @dfeuer says, include the predicate and the function in mapping's signature.
mapping :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapping _ _ =
mapping p f (a:as)
| p a = f a: mapping p f as
| otherwise = a:mapping p f as
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
The part that seems like bad design to me is that you fail to factor out the basic pattern. As assembly.jc has pointed out, you should really give that its own function.
mapOnly :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapOnly p f = map $ x -> if p x then f x else x
Using if seems quite reasonable style in this context; anything else really comes off as clunky.
add a comment |
up vote
5
down vote
The part that seems like bad design to me is that you fail to factor out the basic pattern. As assembly.jc has pointed out, you should really give that its own function.
mapOnly :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapOnly p f = map $ x -> if p x then f x else x
Using if seems quite reasonable style in this context; anything else really comes off as clunky.
add a comment |
up vote
5
down vote
up vote
5
down vote
The part that seems like bad design to me is that you fail to factor out the basic pattern. As assembly.jc has pointed out, you should really give that its own function.
mapOnly :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapOnly p f = map $ x -> if p x then f x else x
Using if seems quite reasonable style in this context; anything else really comes off as clunky.
The part that seems like bad design to me is that you fail to factor out the basic pattern. As assembly.jc has pointed out, you should really give that its own function.
mapOnly :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapOnly p f = map $ x -> if p x then f x else x
Using if seems quite reasonable style in this context; anything else really comes off as clunky.
answered Nov 11 at 17:52
dfeuer
32.1k347126
32.1k347126
add a comment |
add a comment |
up vote
0
down vote
If you are force to use guards, is pretty straightforward. I think if .. then .. else is a good pattern here though. As @dfeuer says, include the predicate and the function in mapping's signature.
mapping :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapping _ _ =
mapping p f (a:as)
| p a = f a: mapping p f as
| otherwise = a:mapping p f as
add a comment |
up vote
0
down vote
If you are force to use guards, is pretty straightforward. I think if .. then .. else is a good pattern here though. As @dfeuer says, include the predicate and the function in mapping's signature.
mapping :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapping _ _ =
mapping p f (a:as)
| p a = f a: mapping p f as
| otherwise = a:mapping p f as
add a comment |
up vote
0
down vote
up vote
0
down vote
If you are force to use guards, is pretty straightforward. I think if .. then .. else is a good pattern here though. As @dfeuer says, include the predicate and the function in mapping's signature.
mapping :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapping _ _ =
mapping p f (a:as)
| p a = f a: mapping p f as
| otherwise = a:mapping p f as
If you are force to use guards, is pretty straightforward. I think if .. then .. else is a good pattern here though. As @dfeuer says, include the predicate and the function in mapping's signature.
mapping :: (a -> Bool) -> (a -> a) -> [a] -> [a]
mapping _ _ =
mapping p f (a:as)
| p a = f a: mapping p f as
| otherwise = a:mapping p f as
answered Nov 12 at 7:59
Luis Morillo
73512
73512
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53250487%2fmap-with-condition-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
4
Your example is legal Haskell code. What exactly is your problem? That is, what about the solution you already have doesn’t work?
– Alexis King
Nov 11 at 15:56
2
do you want this: mapping p f xs= map (x-> if p x then f x else x) xs
– assembly.jc
Nov 11 at 16:04
well the code I have now is considered bad design in my class, we need to try to avoid if and use guards and such instead
– Glenn
Nov 11 at 16:10
I'd not consider that bad design at all. Still, if you want to use guards you can use
mapping all sub = map f all where f x | x `elem` sub = function x | otherwise = x, giving a name for the function youmap– chi
Nov 11 at 16:19
4
Note that you are working with lists, and not arrays. This isn't just terminology nitpicking; it is important to keep the difference in mind as you learn to work with Haskell lists.
– duplode
Nov 11 at 16:21