Get first element of list as Maybe vs Maybe elements












1















In Haskell or fp languages we have maybe monad (Option, Some, None). The names could vary.



So lets say I have function returning first element of list or None if empty.



headm  = None()
headm (x:xs) = Some(x)


So now I have function which gets all values from map and keys as maybe:



keyValues ['name','age'] {age:24} // [None, Some(24)]


I know maps are not syntax correct but the code is just for demonstration purpose.



So if I call headm on empty list I get None.



If I call headm of the result above I also get None.



My question is inst this confusing because i get the same result for empty list and if head of the list has none.



Is it good practice and I should check if the list is empty?










share|improve this question




















  • 1





    thanks, i edited the title hope it is better now

    – user2693928
    Jan 3 at 3:51
















1















In Haskell or fp languages we have maybe monad (Option, Some, None). The names could vary.



So lets say I have function returning first element of list or None if empty.



headm  = None()
headm (x:xs) = Some(x)


So now I have function which gets all values from map and keys as maybe:



keyValues ['name','age'] {age:24} // [None, Some(24)]


I know maps are not syntax correct but the code is just for demonstration purpose.



So if I call headm on empty list I get None.



If I call headm of the result above I also get None.



My question is inst this confusing because i get the same result for empty list and if head of the list has none.



Is it good practice and I should check if the list is empty?










share|improve this question




















  • 1





    thanks, i edited the title hope it is better now

    – user2693928
    Jan 3 at 3:51














1












1








1








In Haskell or fp languages we have maybe monad (Option, Some, None). The names could vary.



So lets say I have function returning first element of list or None if empty.



headm  = None()
headm (x:xs) = Some(x)


So now I have function which gets all values from map and keys as maybe:



keyValues ['name','age'] {age:24} // [None, Some(24)]


I know maps are not syntax correct but the code is just for demonstration purpose.



So if I call headm on empty list I get None.



If I call headm of the result above I also get None.



My question is inst this confusing because i get the same result for empty list and if head of the list has none.



Is it good practice and I should check if the list is empty?










share|improve this question
















In Haskell or fp languages we have maybe monad (Option, Some, None). The names could vary.



So lets say I have function returning first element of list or None if empty.



headm  = None()
headm (x:xs) = Some(x)


So now I have function which gets all values from map and keys as maybe:



keyValues ['name','age'] {age:24} // [None, Some(24)]


I know maps are not syntax correct but the code is just for demonstration purpose.



So if I call headm on empty list I get None.



If I call headm of the result above I also get None.



My question is inst this confusing because i get the same result for empty list and if head of the list has none.



Is it good practice and I should check if the list is empty?







functional-programming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 3:51







user2693928

















asked Jan 3 at 2:14









user2693928user2693928

1,8541716




1,8541716








  • 1





    thanks, i edited the title hope it is better now

    – user2693928
    Jan 3 at 3:51














  • 1





    thanks, i edited the title hope it is better now

    – user2693928
    Jan 3 at 3:51








1




1





thanks, i edited the title hope it is better now

– user2693928
Jan 3 at 3:51





thanks, i edited the title hope it is better now

– user2693928
Jan 3 at 3:51












1 Answer
1






active

oldest

votes


















2














If I am understanding you correctly, the premise of your question actually isn't quite right because you would not get None when applying headm to the [None, Some(24)] list you have described. You would instead get Some(None).



To use a Haskell example to make things more concrete, we can define headm as1:



headm :: [a] -> Maybe a
headm = Nothing
headm (x:xs) = Just x


If we apply this to the empty list, we do indeed get Nothing:



ghci> headm 
Nothing


However, if we apply this to the list [Nothing, Just 24]:



ghci> headm [Nothing, Just 24]
Just Nothing


These are two distinct values (which is why they are printed differently) which are not equal to each other. The values Just (Just Nothing), Just (Just (Just Nothing)), etc, also exist and are distinct from each other (they all in fact potentially have different types, depending on how/if their types are constrained).





1Note: headm already exists in the Haskell standard library. It is called listToMaybe and it exists in Data.Maybe.






share|improve this answer


























  • Thanks, i was wrong. Indeed it returns Some(None). But I wonder is there some cases where both returns the same type. And why the function headm is called listToMaybe since it only returns the first element.

    – user2693928
    Jan 3 at 12:34






  • 1





    @user2693928 Such a case is completely impossible, because the empty list case is, by definition, the only case that gives you None. Non-empty lists will always give you Some ... . listToMaybe is called that because it turns a list into a Maybe and there is another function called maybeToList which goes the other direction.

    – David
    Jan 3 at 18:47













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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015516%2fget-first-element-of-list-as-maybe-vs-maybe-elements%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














If I am understanding you correctly, the premise of your question actually isn't quite right because you would not get None when applying headm to the [None, Some(24)] list you have described. You would instead get Some(None).



To use a Haskell example to make things more concrete, we can define headm as1:



headm :: [a] -> Maybe a
headm = Nothing
headm (x:xs) = Just x


If we apply this to the empty list, we do indeed get Nothing:



ghci> headm 
Nothing


However, if we apply this to the list [Nothing, Just 24]:



ghci> headm [Nothing, Just 24]
Just Nothing


These are two distinct values (which is why they are printed differently) which are not equal to each other. The values Just (Just Nothing), Just (Just (Just Nothing)), etc, also exist and are distinct from each other (they all in fact potentially have different types, depending on how/if their types are constrained).





1Note: headm already exists in the Haskell standard library. It is called listToMaybe and it exists in Data.Maybe.






share|improve this answer


























  • Thanks, i was wrong. Indeed it returns Some(None). But I wonder is there some cases where both returns the same type. And why the function headm is called listToMaybe since it only returns the first element.

    – user2693928
    Jan 3 at 12:34






  • 1





    @user2693928 Such a case is completely impossible, because the empty list case is, by definition, the only case that gives you None. Non-empty lists will always give you Some ... . listToMaybe is called that because it turns a list into a Maybe and there is another function called maybeToList which goes the other direction.

    – David
    Jan 3 at 18:47


















2














If I am understanding you correctly, the premise of your question actually isn't quite right because you would not get None when applying headm to the [None, Some(24)] list you have described. You would instead get Some(None).



To use a Haskell example to make things more concrete, we can define headm as1:



headm :: [a] -> Maybe a
headm = Nothing
headm (x:xs) = Just x


If we apply this to the empty list, we do indeed get Nothing:



ghci> headm 
Nothing


However, if we apply this to the list [Nothing, Just 24]:



ghci> headm [Nothing, Just 24]
Just Nothing


These are two distinct values (which is why they are printed differently) which are not equal to each other. The values Just (Just Nothing), Just (Just (Just Nothing)), etc, also exist and are distinct from each other (they all in fact potentially have different types, depending on how/if their types are constrained).





1Note: headm already exists in the Haskell standard library. It is called listToMaybe and it exists in Data.Maybe.






share|improve this answer


























  • Thanks, i was wrong. Indeed it returns Some(None). But I wonder is there some cases where both returns the same type. And why the function headm is called listToMaybe since it only returns the first element.

    – user2693928
    Jan 3 at 12:34






  • 1





    @user2693928 Such a case is completely impossible, because the empty list case is, by definition, the only case that gives you None. Non-empty lists will always give you Some ... . listToMaybe is called that because it turns a list into a Maybe and there is another function called maybeToList which goes the other direction.

    – David
    Jan 3 at 18:47
















2












2








2







If I am understanding you correctly, the premise of your question actually isn't quite right because you would not get None when applying headm to the [None, Some(24)] list you have described. You would instead get Some(None).



To use a Haskell example to make things more concrete, we can define headm as1:



headm :: [a] -> Maybe a
headm = Nothing
headm (x:xs) = Just x


If we apply this to the empty list, we do indeed get Nothing:



ghci> headm 
Nothing


However, if we apply this to the list [Nothing, Just 24]:



ghci> headm [Nothing, Just 24]
Just Nothing


These are two distinct values (which is why they are printed differently) which are not equal to each other. The values Just (Just Nothing), Just (Just (Just Nothing)), etc, also exist and are distinct from each other (they all in fact potentially have different types, depending on how/if their types are constrained).





1Note: headm already exists in the Haskell standard library. It is called listToMaybe and it exists in Data.Maybe.






share|improve this answer















If I am understanding you correctly, the premise of your question actually isn't quite right because you would not get None when applying headm to the [None, Some(24)] list you have described. You would instead get Some(None).



To use a Haskell example to make things more concrete, we can define headm as1:



headm :: [a] -> Maybe a
headm = Nothing
headm (x:xs) = Just x


If we apply this to the empty list, we do indeed get Nothing:



ghci> headm 
Nothing


However, if we apply this to the list [Nothing, Just 24]:



ghci> headm [Nothing, Just 24]
Just Nothing


These are two distinct values (which is why they are printed differently) which are not equal to each other. The values Just (Just Nothing), Just (Just (Just Nothing)), etc, also exist and are distinct from each other (they all in fact potentially have different types, depending on how/if their types are constrained).





1Note: headm already exists in the Haskell standard library. It is called listToMaybe and it exists in Data.Maybe.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 9:05

























answered Jan 3 at 8:53









DavidDavid

9,64422544




9,64422544













  • Thanks, i was wrong. Indeed it returns Some(None). But I wonder is there some cases where both returns the same type. And why the function headm is called listToMaybe since it only returns the first element.

    – user2693928
    Jan 3 at 12:34






  • 1





    @user2693928 Such a case is completely impossible, because the empty list case is, by definition, the only case that gives you None. Non-empty lists will always give you Some ... . listToMaybe is called that because it turns a list into a Maybe and there is another function called maybeToList which goes the other direction.

    – David
    Jan 3 at 18:47





















  • Thanks, i was wrong. Indeed it returns Some(None). But I wonder is there some cases where both returns the same type. And why the function headm is called listToMaybe since it only returns the first element.

    – user2693928
    Jan 3 at 12:34






  • 1





    @user2693928 Such a case is completely impossible, because the empty list case is, by definition, the only case that gives you None. Non-empty lists will always give you Some ... . listToMaybe is called that because it turns a list into a Maybe and there is another function called maybeToList which goes the other direction.

    – David
    Jan 3 at 18:47



















Thanks, i was wrong. Indeed it returns Some(None). But I wonder is there some cases where both returns the same type. And why the function headm is called listToMaybe since it only returns the first element.

– user2693928
Jan 3 at 12:34





Thanks, i was wrong. Indeed it returns Some(None). But I wonder is there some cases where both returns the same type. And why the function headm is called listToMaybe since it only returns the first element.

– user2693928
Jan 3 at 12:34




1




1





@user2693928 Such a case is completely impossible, because the empty list case is, by definition, the only case that gives you None. Non-empty lists will always give you Some ... . listToMaybe is called that because it turns a list into a Maybe and there is another function called maybeToList which goes the other direction.

– David
Jan 3 at 18:47







@user2693928 Such a case is completely impossible, because the empty list case is, by definition, the only case that gives you None. Non-empty lists will always give you Some ... . listToMaybe is called that because it turns a list into a Maybe and there is another function called maybeToList which goes the other direction.

– David
Jan 3 at 18:47






















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015516%2fget-first-element-of-list-as-maybe-vs-maybe-elements%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







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas