Re: [Haskell-cafe] n00b question: defining datatype
On Thu, Jul 23, 2009 at 08:17:34PM +0100, Iain Barnett wrote: [..] against the empty list it's not really a problem to have it there. I didn't realise I could use Maybe in the constructor because it's a monad, but that's good because I was wondering about the best way to make a nullable value. Actually, this has nothing to do with Maybe being a monad. The reason you can do this is because Maybe itself is not a type, but a (unary) type constructor(It has kind * - *), so you need to apply it to another type. Doing something like test :: Maybe would be an error. That Data.Tree module looks interesting too! It does seem to be a naturally recursive type, but I'm still trying to become easy with that sort of thought :) [..] A list is also recursively defined, so it is not really more difficult to use a Tree instead. E.g. one could define a list type like this: data List a = Nil | Cons a (List a) - Daniel pgpx25fhhGIwn.pgp Description: PGP signature ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] n00b question: defining datatype
Hi, I'm trying to get my head around datatypes, and wondering how I might define a simple Task datatype in Haskell. data Task = Task { title :: String, completed :: Bool } Ok, that's straightforward, but sometimes tasks become a list of tasks themselves data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have subtasks. So I try this: data Task = Task { title :: String, completed :: Bool } | TaskWithSubtasks { title :: String, completed :: Bool, subtasks :: [Task] } It's a bit more accurate, but it's repeating things, which is ok with a simple type. Could anyone suggest a better way to define this? If I was using C#, which I'm far more familiar with, I could overload the constructor and refer to the smaller constructor. Is there a way to do that in Haskell, or am I still thinking too OOP? Iain ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
Iain Barnett wrote: data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } This one looks the best to me. Remember, you can just use an empty list if the task has no subtasks. - Jake ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
On Thu, Jul 23, 2009 at 8:43 PM, Iain Barnettiainsp...@gmail.com wrote: data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have subtasks. The empty list is a list. --Max ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
Consider data Task = Task { title :: String, completed :: Bool, subtasks :: Maybe [Task] } Iain Barnett wrote: Hi, I'm trying to get my head around datatypes, and wondering how I might define a simple Task datatype in Haskell. data Task = Task { title :: String, completed :: Bool } Ok, that's straightforward, but sometimes tasks become a list of tasks themselves data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have subtasks. So I try this: data Task = Task { title :: String, completed :: Bool } | TaskWithSubtasks { title :: String, completed :: Bool, subtasks :: [Task] } It's a bit more accurate, but it's repeating things, which is ok with a simple type. Could anyone suggest a better way to define this? If I was using C#, which I'm far more familiar with, I could overload the constructor and refer to the smaller constructor. Is there a way to do that in Haskell, or am I still thinking too OOP? Iain ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- View this message in context: http://www.nabble.com/n00b-question%3A-defining-datatype-tp24631976p24632019.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
Hello Iain, Thursday, July 23, 2009, 10:43:02 PM, you wrote: data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have subtasks. don't see a problem - subtasks list may be empty -- Best regards, Bulatmailto:bulat.zigans...@gmail.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
data Task = Task { title :: String, completed :: Bool, subtasks :: [Task] } But that's not really right, because obviously, some tasks don't have As Jake said - that's fine and you can still pattern match on the null ([]) taskes when looking for tasks without subtaskes. data Task = Task { title :: String, completed :: Bool } | TaskWithSubtasks { title :: String, completed :: Bool, subtasks :: [Task] } It's a bit more accurate, but it's repeating things, which is ok with a simple type. Could anyone suggest a better way to define this? If I was using C#, which I'm far more familiar with, I could overload the constructor and refer to the smaller constructor. So do you want: data Job = Job {title :: String, completed :: Bool } data Task = Task Job | MasterTask String [Job] Tom ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
Actually, how about this? import Data.Tree newtype Task = Task (Tree (String, Bool)) Now you already have that tree structure you wanted. - Jake ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
Ok, thanks to everyone, that's certainly answered my question and given me some more avenues to pursue. I can see now that because I can pattern match against the empty list it's not really a problem to have it there. I didn't realise I could use Maybe in the constructor because it's a monad, but that's good because I was wondering about the best way to make a nullable value. That Data.Tree module looks interesting too! It does seem to be a naturally recursive type, but I'm still trying to become easy with that sort of thought :) Thanks for all the help, it's nice to get a bit of feedback when still getting used to things. Iain ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] n00b question: defining datatype
Kim-Ee Yeoh wrote: Consider data Task = Task { title :: String, completed :: Bool, subtasks :: Maybe [Task] } Note that unless you have some meaning in mind for the difference between a subtask value of Nothing vs. (Just []), the Maybe is redundant. Anton ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] N00b question
poop wrote: So I'm working my way thorough haskell, doing some programming problems, and I have this one so far: Hi, I haven't spotted the problem in your code but there's an alternative solution to Euler Problem 14 on the wiki: http://www.haskell.org/haskellwiki/Euler_problems/11_to_20#Problem_14 -- it may be helpful as a comparison? Regards, p14next n = if (mod n 2 == 0) then (div n 2) else (3*n + 1) p14seqlen n = f' 1 n where f' accum n | n == 1= accum | otherwise = f' (accum + 1) (p14next n) sndmax a b = if snd a snd b then a else b p14 n = fst (f' (0,0) n) where f' maxTuple n | n == 0= maxTuple | otherwise = f' (sndmax maxTuple (n, p14seqlen n)) (n-1) the goal is to be able to run: p14 99 and not get a stack overflow message. I read what was available on tail recursion and I think I have it working with tail recursion now, at least it gives me a stack overflow message really quickly compared to before :) I have not been able to find any documentation on seq and ($!) which was also mentioned as a way to get rid of stack overflow messages so if those are what is required an explanation of those would be most helpful. -- View this message in context: http://www.nabble.com/N00b-question-tf4010616.html#a11395620 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe