|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
||||
|
||||
|
Starting out with haskell.
Hi,
I just started in haskell, but couldn't find much material, I have two books #1 Haskell Road to Logic Math and Programming #2 Craft of Haskell Programming. If anyone has any more info on how to approach haskell and take it to the next level it will be much appreciated .. thanks in advance...
__________________
The only Verdict is Vengeance a Vendetta held as a Votive, not in Vain, for the Value and Veracity of such shall one day Vindicate the Vigilant and the Virtuous Mav RLZ AC/DC RLZ |
|
#2
|
||||
|
||||
|
Quote:
thing is while I can tell whether or not that integer n is present or not, Code:
removeFst :: Int -> [Int] -> bool removeFst n [] = error "your list is empty" removeFst n [x] | n == x = True | otherwise = False removeFst n (x:xs) | n == x = True | otherwise = removeFst n xs how do I remove that element. I tried replacing True with Code:
Delete n [x] Thanks in advance ... |
|
#3
|
|||
|
|||
|
Doing exercises like this is good practice, and so is reading the Prelude - `delete' is defined there like this:
Code:
-- | 'delete' @x@ removes the first occurrence of @x@ from its list argument. -- For example, -- -- > delete 'a' "banana" == "bnana" -- -- It is a special case of 'deleteBy', which allows the programmer to -- supply their own equality test. delete :: (Eq a) => a -> [a] -> [a] delete = deleteBy (==) -- | The 'deleteBy' function behaves like 'delete', but takes a -- user-supplied equality predicate. deleteBy :: (a -> a -> Bool) -> a -> [a] -> [a] deleteBy _ _ [] = [] deleteBy eq x (y:ys) = if x `eq` y then ys else y : deleteBy eq x ys Use hoogle.com too, to find functions by their type signatures. You know this must require instances of Eq, and is a function from lists to lists. Lo and behold, hoogling "Eq a => [a] -> [a]" gets results. Last edited by jamieB : June 26th, 2007 at 06:46 AM. |
|
#4
|
|||
|
|||
|
Quote:
Keep at it, write some code, set yourself goals for actual programs you want to write. E.g. I tried the ruby quiz puzzles in haskell (http://www.rubyquiz.com/) and put some solutions on the haskell wiki. And look out for Don Stewarts O'Reilly book Practical Haskell (later in the year?). |
|
#5
|
|||
|
|||
|
Quote:
Err, sorry - delete is in Data.List, not the prelude. I got that source code by following the link from hoogle and clicking on source code in the haddock docs. You should get in the habit of doing that. |
|
#6
|
||||
|
||||
|
Code:
{- creating a new list without n -}
delete' :: a -> [a] -> [a]
delete' n [] = error "your list is empty"
delete' n [x] | n == x = []
| otherwise = [x]
delete' n (x:xs) | n == x = delete' x xs
| otherwise = x:delete' n xs
I got the above code working which basically removes elements in the list ==n Hey feels good to know that there are people who know haskell, was beginning to think otherwise .I am currently referring Haskell Road to Logic Math and Programming, and just started chapter 2, its a good book with the right mix of solved and unsolved problems. #1 Well there was this exercise where I had to write a function blowup() that would do as follows >> i/p "abcd" o/p "abbcccdddd" and this is my solution Code:
multiply' :: Char -> Int -> String multiply' x y | y == 0 = [] | otherwise = x : multiply' x (y-1) blowUP :: Int -> String -> String blowUP y [] = [] blowUP y (x:xs) = let ctr = y + 1 ch = x in ( multiply' ch ctr ) ++ ( blowUP ctr xs ) getString' :: String -> String getString' [] = [] getString' [x] = [x] getString' xs = blowUP 0 xs The problem here is that I have to write an extra function getString' just to initialize the variable ctr ( which basically tells the number of times the char is to be repeated). Well in c++ I would use a global variable or preferrably static variables. What to do in haskell. Mav ... |
|
#7
|
|||
|
|||
|
Quote:
The Road to Logic was the first Haskell book I read -- I think it's excellent too. One mistake you're making above is in pattern matching - you only need to match [] and (x:xs) because [x] matches (x:xs) where xs == [] (you still need to match [x] if you need to handle the singleton list specially). You do need a helper function of some sort I think. Here's a solution: Code:
blowup :: [a] -> [a]
blowup xs = blowup' xs 1
where blowup' [] _ = []
blowup' (x:xs) n = take n (repeat x) ++ blowup' xs (n+1)
My advice is to post your future questions to the haskell-cafe mailing list - you'll get inundated with high quality help! Good luck with the book and with haskell. |
|
#8
|
||||
|
||||
|
Thanks man..
but where is this haskell-cafe mailing list ![]() |
|
#9
|
|||
|
|||
|
|
|
#10
|
|||
|
|||
|
Quote:
You can always do without a helper function if you really want to: Code:
blowup = foldr (\(n,x) ys -> (take n (repeat x)) ++ ys) [] . zip [1..] Also, anything you can write with direct recursion (a base case for the empty list, or zero etc, and a case for everything else) can be written with a higher order recursive function like foldr, and it's often a good idea to do so even if it's sometimes less easy to read. Not that it matters in this case...... Last edited by jamieB : June 27th, 2007 at 02:41 PM. |
|
#11
|
|||
|
|||
|
I'm chatting away to myself here, but this reminds of something funny - evolution of a haskell programmer
![]() |
|
#12
|
||||
|
||||
|
I had posted this thread at nabble, but then I checked a couple of days later that I had received an e-mail from nabble that I wasn't eligible for the mailing list, is this because I posted in haskell-haskell instead of haskell-cafe ?
I am not going to join any IRC anytime soon, lest they roast me! |
|
#13
|
|||
|
|||
|
Quote:
|