
What would be an elegant way of writing this computeHead :: (a -> [b]) -> [a] -> [b] Where when [a] is null, it returns a null list, but when [a] contains one or more elements, it applies the given function to the head of a and returns that? Is there some existing typeclass operator that facilitates this? You can write computeHead _ [] = [] computeHead f (x:_) = f x But that first line seems suspicious to me... it makes me think about how in the list Monad, an empty list falls through. But I can't quite make it work.

How about: computeHead f = take 1 . fmap f Martin Dennis Raddle:
What would be an elegant way of writing this
computeHead :: (a -> [b]) -> [a] -> [b]
Where when [a] is null, it returns a null list, but when [a] contains one or more elements, it applies the given function to the head of a and returns that? Is there some existing typeclass operator that facilitates this?
You can write
computeHead _ [] = [] computeHead f (x:_) = f x
But that first line seems suspicious to me... it makes me think about how in the list Monad, an empty list falls through. But I can't quite make it work.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Oh, and a little background. fmap comes from the Functor typeclass, and lists have an instance of Functor so the empty list case is handled automatically. Then I used "take 1" to get the head in a safe way, e.g. defaulting to empty list if the output of fmap is empty. M. Martin Vlk:
How about:
computeHead f = take 1 . fmap f
Martin
Dennis Raddle:
What would be an elegant way of writing this
computeHead :: (a -> [b]) -> [a] -> [b]
Where when [a] is null, it returns a null list, but when [a] contains one or more elements, it applies the given function to the head of a and returns that? Is there some existing typeclass operator that facilitates this?
You can write
computeHead _ [] = [] computeHead f (x:_) = f x
But that first line seems suspicious to me... it makes me think about how in the list Monad, an empty list falls through. But I can't quite make it work.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Just after I posted that question, I started driving home, and on the drive I thought of your answer. I think I'm starting to ask the right questions when I'm programming in Haskell. Like redundancy and bloat is a sure sign that a more witty expression is available, and that I should consult the typeclasses. Second, I am not used to the implications of laziness, so it took me a while to hit on your solution because I keep thinking you have to map something over the whole list, and that if you only want to map it over the head, you are stuck. You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for lists? D

map is specialized for lists while fmap is for any functors. Its presence
is historical. Prefer fmap over map.
On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle
Just after I posted that question, I started driving home, and on the drive I thought of your answer. I think I'm starting to ask the right questions when I'm programming in Haskell. Like redundancy and bloat is a sure sign that a more witty expression is available, and that I should consult the typeclasses.
Second, I am not used to the implications of laziness, so it took me a while to hit on your solution because I keep thinking you have to map something over the whole list, and that if you only want to map it over the head, you are stuck.
You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for lists?
D
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm
have very good answers on this.
On Thu, Nov 12, 2015 at 7:11 PM, akash g
map is specialized for lists while fmap is for any functors. Its presence is historical. Prefer fmap over map.
On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle
wrote: Just after I posted that question, I started driving home, and on the drive I thought of your answer. I think I'm starting to ask the right questions when I'm programming in Haskell. Like redundancy and bloat is a sure sign that a more witty expression is available, and that I should consult the typeclasses.
Second, I am not used to the implications of laziness, so it took me a while to hit on your solution because I keep thinking you have to map something over the whole list, and that if you only want to map it over the head, you are stuck.
You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for lists?
D
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Speaking of lists and history, I noticed that a lot of library functions
which were formerly defined over lists (when I first looked at Haskell six
years ago) are now defined on Traversable, which makes it a lot harder for
beginners to read the documentation. I have been playing with Haskell for
five years, but not much, so I'm still a beginner. I just mentally
substitute lists when I see Traversable.
I only really use lists and Maybe, as far as instances of the typeclasses
go. That's only two types, but a lot to learn!
D
On Thu, Nov 12, 2015 at 5:42 AM, akash g
http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm have very good answers on this.
On Thu, Nov 12, 2015 at 7:11 PM, akash g
wrote: map is specialized for lists while fmap is for any functors. Its presence is historical. Prefer fmap over map.
On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle
wrote: Just after I posted that question, I started driving home, and on the drive I thought of your answer. I think I'm starting to ask the right questions when I'm programming in Haskell. Like redundancy and bloat is a sure sign that a more witty expression is available, and that I should consult the typeclasses.
Second, I am not used to the implications of laziness, so it took me a while to hit on your solution because I keep thinking you have to map something over the whole list, and that if you only want to map it over the head, you are stuck.
You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for lists?
D
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I think these changes came with GHC 7.10. I also completely agree that
this is difficult for beginners. They should've had a beginner's Prelude
or something :)
On Thu, Nov 12, 2015 at 7:18 PM, Dennis Raddle
Speaking of lists and history, I noticed that a lot of library functions which were formerly defined over lists (when I first looked at Haskell six years ago) are now defined on Traversable, which makes it a lot harder for beginners to read the documentation. I have been playing with Haskell for five years, but not much, so I'm still a beginner. I just mentally substitute lists when I see Traversable.
I only really use lists and Maybe, as far as instances of the typeclasses go. That's only two types, but a lot to learn!
D
On Thu, Nov 12, 2015 at 5:42 AM, akash g
wrote: http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm have very good answers on this.
On Thu, Nov 12, 2015 at 7:11 PM, akash g
wrote: map is specialized for lists while fmap is for any functors. Its presence is historical. Prefer fmap over map.
On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle
wrote: Just after I posted that question, I started driving home, and on the drive I thought of your answer. I think I'm starting to ask the right questions when I'm programming in Haskell. Like redundancy and bloat is a sure sign that a more witty expression is available, and that I should consult the typeclasses.
Second, I am not used to the implications of laziness, so it took me a while to hit on your solution because I keep thinking you have to map something over the whole list, and that if you only want to map it over the head, you are stuck.
You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for lists?
D
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

There has been discussion on the changes to the Prelude. Just found this (
https://wiki.haskell.org/Foldable_Traversable_In_Prelude).
On Thu, Nov 12, 2015 at 7:57 PM, akash g
I think these changes came with GHC 7.10. I also completely agree that this is difficult for beginners. They should've had a beginner's Prelude or something :)
On Thu, Nov 12, 2015 at 7:18 PM, Dennis Raddle
wrote: Speaking of lists and history, I noticed that a lot of library functions which were formerly defined over lists (when I first looked at Haskell six years ago) are now defined on Traversable, which makes it a lot harder for beginners to read the documentation. I have been playing with Haskell for five years, but not much, so I'm still a beginner. I just mentally substitute lists when I see Traversable.
I only really use lists and Maybe, as far as instances of the typeclasses go. That's only two types, but a lot to learn!
D
On Thu, Nov 12, 2015 at 5:42 AM, akash g
wrote: http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm have very good answers on this.
On Thu, Nov 12, 2015 at 7:11 PM, akash g
wrote: map is specialized for lists while fmap is for any functors. Its presence is historical. Prefer fmap over map.
On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle
wrote:
Just after I posted that question, I started driving home, and on the drive I thought of your answer. I think I'm starting to ask the right questions when I'm programming in Haskell. Like redundancy and bloat is a sure sign that a more witty expression is available, and that I should consult the typeclasses.
Second, I am not used to the implications of laziness, so it took me a while to hit on your solution because I keep thinking you have to map something over the whole list, and that if you only want to map it over the head, you are stuck.
You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for lists?
D
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

There was some real controversy over this change. People were strongly
opposed to the change for exactly the reason you gave (among others), that
it is hard for beginners. However, there was enough momentum that it went
through, and now things like fmap and length work on a wide variety of data
types. You can google ftp haskell controversy for more info.
On Thu, Nov 12, 2015 at 8:48 AM, Dennis Raddle
Speaking of lists and history, I noticed that a lot of library functions which were formerly defined over lists (when I first looked at Haskell six years ago) are now defined on Traversable, which makes it a lot harder for beginners to read the documentation. I have been playing with Haskell for five years, but not much, so I'm still a beginner. I just mentally substitute lists when I see Traversable.
I only really use lists and Maybe, as far as instances of the typeclasses go. That's only two types, but a lot to learn!
D
On Thu, Nov 12, 2015 at 5:42 AM, akash g
wrote: http://stackoverflow.com/questions/7463500/why-do-we-have-map-fmap-and-liftm have very good answers on this.
On Thu, Nov 12, 2015 at 7:11 PM, akash g
wrote: map is specialized for lists while fmap is for any functors. Its presence is historical. Prefer fmap over map.
On Thu, Nov 12, 2015 at 7:03 PM, Dennis Raddle
wrote: Just after I posted that question, I started driving home, and on the drive I thought of your answer. I think I'm starting to ask the right questions when I'm programming in Haskell. Like redundancy and bloat is a sure sign that a more witty expression is available, and that I should consult the typeclasses.
Second, I am not used to the implications of laziness, so it took me a while to hit on your solution because I keep thinking you have to map something over the whole list, and that if you only want to map it over the head, you are stuck.
You can use 'map' also, instead of 'fmap', right? Is 'map' just 'fmap' for lists?
D
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Thu, Nov 12, 2015 at 6:32 AM, David McBride
There was some real controversy over this change. People were strongly opposed to the change for exactly the reason you gave (among others), that it is hard for beginners. However, there was enough momentum that it went through, and now things like fmap and length work on a wide variety of data types. You can google ftp haskell controversy for more info.
Thanks, David. As far as instances of Functor, Applicative, and Monad, so far I've mostly used lists and Maybe. I've never taken specific advantage of Traversable. Could you suggest a new type for me to explore? I mean something that could have instances in any of the above classes, one or all. I only use Haskell for an occasional hobby project so despite having started five years ago, I consider myself a beginner. My project involves playing music by transforming a musical score into MIDI commands and sending them to a software synthesizer. As far as library types, I mostly use Map, lists, and Maybe. These are all I have needed to look for patterns in lists or Maps of notes. But I'm sure I'm missing out on something. D

It is mostly just things like:
Prelude.length (Data.Map.fromList [(1,"1"),(2,"2")])
Prelude.foldr (+) 0 (Data.Set.fromList [1,2,3,4,5])
Prelude.null Nothing
Prelude.null (Just 2)
Just watch out for some gotchas:
minimum (1,2)
On Thu, Nov 12, 2015 at 4:00 PM, Dennis Raddle
On Thu, Nov 12, 2015 at 6:32 AM, David McBride
wrote: There was some real controversy over this change. People were strongly opposed to the change for exactly the reason you gave (among others), that it is hard for beginners. However, there was enough momentum that it went through, and now things like fmap and length work on a wide variety of data types. You can google ftp haskell controversy for more info.
Thanks, David. As far as instances of Functor, Applicative, and Monad, so far I've mostly used lists and Maybe. I've never taken specific advantage of Traversable.
Could you suggest a new type for me to explore? I mean something that could have instances in any of the above classes, one or all.
I only use Haskell for an occasional hobby project so despite having started five years ago, I consider myself a beginner. My project involves playing music by transforming a musical score into MIDI commands and sending them to a software synthesizer. As far as library types, I mostly use Map, lists, and Maybe. These are all I have needed to look for patterns in lists or Maps of notes. But I'm sure I'm missing out on something.
D
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Oh I needed join.
computeHead :: (a -> [b]) -> [a] -> [b]
computeHead f = join . take 1 . fmap f
That's because
f :: a -> [b]
not
f: a -> b
On Thu, Nov 12, 2015 at 5:22 AM, Martin Vlk
How about:
computeHead f = take 1 . fmap f
Martin
Dennis Raddle:
What would be an elegant way of writing this
computeHead :: (a -> [b]) -> [a] -> [b]
Where when [a] is null, it returns a null list, but when [a] contains one or more elements, it applies the given function to the head of a and returns that? Is there some existing typeclass operator that facilitates this?
You can write
computeHead _ [] = [] computeHead f (x:_) = f x
But that first line seems suspicious to me... it makes me think about how in the list Monad, an empty list falls through. But I can't quite make it work.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

With the join it's the same thing as:
computeHead :: (a -> [b]) -> [a] -> [b]
computeHead f xs = take 1 xs >>= f
On Thu, 12 Nov 2015 at 06:58 Dennis Raddle
Oh I needed join.
computeHead :: (a -> [b]) -> [a] -> [b] computeHead f = join . take 1 . fmap f
That's because
f :: a -> [b]
not
f: a -> b
On Thu, Nov 12, 2015 at 5:22 AM, Martin Vlk
wrote: How about:
computeHead f = take 1 . fmap f
Martin
Dennis Raddle:
What would be an elegant way of writing this
computeHead :: (a -> [b]) -> [a] -> [b]
Where when [a] is null, it returns a null list, but when [a] contains one or more elements, it applies the given function to the head of a and returns that? Is there some existing typeclass operator that facilitates this?
You can write
computeHead _ [] = [] computeHead f (x:_) = f x
But that first line seems suspicious to me... it makes me think about how in the list Monad, an empty list falls through. But I can't quite make it work.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (5)
-
akash g
-
Alex Hammel
-
David McBride
-
Dennis Raddle
-
Martin Vlk