« Return to Thread: School assignment help

School assignment help

by Urmastaag :: Rate this Message:

Reply to Author | View in Thread

Hello!

Can anybody point me to a right way here?
I have those exercises in haskell that i cannot do, because haskell is so much different from other languages that i use. I have read so much from different manuals but i do not understand haskell enough yet.

First exercise:

I must find a shorter list.
Here's how i did that, but my teacher said that it is unefficient way to do it.

getShorter :: [a] -> [a] -> [a]
getShorter xs ys | length xs <= length ys = xs
                       | otherwise                  = ys

How do i do it effective, so that i only check as many elements from lists as many are in the shortest list?

Second exercise:

Second exercise also need an effective answer:

I have to remove any occurances of certain number from list and give the number of how many times that number was removed.

Unefficient version:
remCount :: Int -> [Int] -> (Int,[Int])
remCount n xs = (length xs - length ys, ys)
       where ys = [ x | x <- xs, x /= n]



Exercise three:

moneyBack :: Int -> [(Int,Int)]

I must print out all pairs of bank notes and (bank notes, how many) which salesman would give me back when he/she has to give me like 456 back.

moneyBack 0        ==>   []
moneyBack 10      ==>   [(10,1)]
moneyBack 123     ==>   [(100,1),(10,2),(2,1),(1,1)]
moneyBack 5326   ==>   [(500,10),(100,3),(25,1),(1,1)]

If anybody could point me in a right direction that would be great.
Haskell seems very cool because the code is usually really short, but i just need a start. I need more examples than i can find from google (maybe i'm not using the right keywords).

Thanks.

 « Return to Thread: School assignment help