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

Welcome to Dream.In.Code
Become an Expert!

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




Haskell question

 

Haskell question

srose34

10 Oct, 2009 - 11:27 AM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2009
Posts: 4

Hi
I have to write a Function which goes by the following:
CODE
Int -> [Char] -> [Char]


The function takes the user inputted integer and takes that number of characters of the start of the list and moves them to the end of that list. The Function is meant to return an error if the number is less than 0 or greater than the list of characters. How do I implement the error?

Here is what I have without the error part:
CODE
rotate x str = drop x str ++ take x str


Please help

Thanks

This post has been edited by srose34: 10 Oct, 2009 - 11:52 AM

User is offlineProfile CardPM
+Quote Post


mostyfriedman

RE: Haskell Question

11 Oct, 2009 - 10:12 AM
Post #2

Striving Student
Group Icon

Joined: 24 Oct, 2008
Posts: 3,214



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

My Contributions
in haskell there is an error function called error which takes a string as an argument and returns that string..so you can check if the number is less then 0 and call the function..someting like this
CODE

rotate x string | x < 0 = error "the number you entered is invalid"
--rest of the code here

User is online!Profile CardPM
+Quote Post

Raynes

RE: Haskell Question

13 Oct, 2009 - 05:21 AM
Post #3

Resident Witch. No, really.
Group Icon

Joined: 5 Jan, 2009
Posts: 957



Thanked: 14 times
Dream Kudos: 250
My Contributions
mostyfriedman is right. I will note however, that the 'error' function will bail out of your program immediately. If the error is absolutely catastrophic, and there is no returning from it, the error function is a good choice. However, if you just want to throw an exception, but let the program continue, I suggest you check out the Control.Exception module: http://haskell.org/ghc/docs/latest/html/li...-Exception.html

May the functions be with you.
User is offlineProfile CardPM
+Quote Post

srose34

RE: Haskell Question

13 Oct, 2009 - 11:55 AM
Post #4

New D.I.C Head
*

Joined: 10 Oct, 2009
Posts: 4

Hi
Thanks for the replies. i got it working now.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:05PM

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