School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,124 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,029 people online right now. Registration is fast and FREE... Join Now!




haskell

 

haskell

hellhound

10 Aug, 2009 - 06:13 PM
Post #1

D.I.C Head
**

Joined: 7 Mar, 2009
Posts: 54

hi again another haskell question for you. with this one im quite lost and dont even know if this is how you do it

anyway this is the question i have:
Write a function that takes as input a time as an integer number of seconds after midnight and
converts it to the number of hours, minutes and seconds written as a triple: (hours,
minutes, seconds). Call your function time.
For example, time 50004 should evaluate to (13, 53, 24).


and this is the code that i have written so far please dont laugh....

CODE

type Settings = (Int, Int, Int) -- (hours, Minutes, Seconds)
time :: Int -> Settings
time (_,_,x) = x/360
time (_,x,_) = (x `mod` 360)/60
time (x,_,_) = (x `mod` 60)



i would normally have a week to go over this and try and work it out more myself but i have my grandfathers funeral to go to on thursday so if you could point me in the right direction or even somewere that explains triples better than my teacher does that would be great.

also on a side note is there any easy way of finding out how many digits are in a number without using recursive x/10?

This post has been edited by hellhound: 10 Aug, 2009 - 06:34 PM

User is offlineProfile CardPM
+Quote Post


mostyfriedman

RE: Haskell

11 Aug, 2009 - 02:23 PM
Post #2

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 3,214



Thanked: 357 times
Dream Kudos: 600
Expert In: Learning

My Contributions
hey, sorry for your grandfather.. may he rest in peace..

as for your question..

the question says that the function is supposed to take an integer as input and outputs a tuple..you have correctly defined the type of the function and the type of the Settings..but in the actual function you were doing the opposite, you were inputting a tuple and outputting a numeric value..


this should work for you smile.gif, you can do it in one line like this
CODE

type Settings = (Int, Int, Int) -- (hours, Minutes, Seconds)
time :: Int -> Settings

time x =  (div x 3600, div (mod x 3600) 60, mod x 60)


i hope this helps, if you need more explanation then please ask smile.gif..

as for knowing how many digits are in an integer in haskell, the only way that i know of is recursively dividing by 10 until hitting 0..there maybe a better and more efficient way to do this but this is the only way i know of...
User is online!Profile CardPM
+Quote Post

hellhound

RE: Haskell

11 Aug, 2009 - 09:08 PM
Post #3

D.I.C Head
**

Joined: 7 Mar, 2009
Posts: 54

awesome thanks for that, glad to know that I was kind of on the right track.

now i have another question for you if you don't mind. I am working on the number of digits problems. It is meant to return true if the value has an even number of digits and false if it has an odd number of digits. can you point out where i have gone wrong so far lol.

CODE

even :: Int -> Bool
even n
    |n < 0 = error "value negitive"
    |n == 0 = error "value is 0"
    |(number_digits n 0) `mod` 2 == 0 = True
    |otherwise = False
    
number_digits :: Int -> Int -> Int
number_digits n i
    |n == 0 = 0
    |otherwise = 1 + number_digits (n `mod` 10) (i+1)


ok dont worry i got it sorted.

This post has been edited by hellhound: 11 Aug, 2009 - 09:20 PM
User is offlineProfile CardPM
+Quote Post

mostyfriedman

RE: Haskell

12 Aug, 2009 - 01:02 AM
Post #4

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 3,214



Thanked: 357 times
Dream Kudos: 600
Expert In: Learning

My Contributions
you dont really need a second argument in the numberdigits function, one is enough and you should use division instead of modulus

CODE

numberdigits :: Int -> Int
numberdigits n
    |n == 0 = 0
    |otherwise = 1 + numberdigits (n `div` 10)

User is online!Profile CardPM
+Quote Post

hellhound

RE: Haskell

12 Aug, 2009 - 02:33 AM
Post #5

D.I.C Head
**

Joined: 7 Mar, 2009
Posts: 54

yea i was gona do it this way except i ran into problems as we have to return a bool statment rather than an int.
User is offlineProfile CardPM
+Quote Post

mostyfriedman

RE: Haskell

12 Aug, 2009 - 11:52 AM
Post #6

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 3,214



Thanked: 357 times
Dream Kudos: 600
Expert In: Learning

My Contributions
you can use another function that returns a bool that would call the function that computes the number of digits
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 02:09PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month