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.










share|improve this question



















  • 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 you map
    – 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














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.










share|improve this question



















  • 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 you map
    – 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












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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




    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




    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 you map
    – 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












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.






share|improve this answer



























    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





    share|improve this answer




















      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%2f53250487%2fmap-with-condition-in-haskell%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
      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.






      share|improve this answer
























        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.






        share|improve this answer






















          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 11 at 17:52









          dfeuer

          32.1k347126




          32.1k347126






















              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





              share|improve this answer
























                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





                share|improve this answer






















                  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





                  share|improve this answer












                  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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 7:59









                  Luis Morillo

                  73512




                  73512



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands