HELP NEEDED
Submitted by babyneri on Thu, 07/28/2005 - 12:24pm.
::
can anyone help with with these questions
1. Given two values say, A and B, display the sum of all odd numbers between A and B exclusive.
2. Given a number X, display the factorial of X
3. Given two characters display the larger of the two.
4. Given a list of integers display the smallest number in the list
5. Given a list of characters display True or False if the list contains capital letters
6. Given a list of integers display the sum of all numbers in the list
7. Given a list of integers display the sum of all even numbers
my teacher ran through the course without teaching it properly.....can anyone help me write it as simply as possible?
please email me if you can: risha15@yahoo.coom
- babyneri's blog
- Login to post comments
I don't think this is worth commenting, but if you can't come up with an answer to those simple questions, or at least try it and post some code you used to try and find a solution, we are certainly not going to solve them for you.
Homework assignments are exactly that: stuff YOU should do, not the rest of the world.
Grr.
Here are some useful functions which you should know about while attempting these problems:
filter :: (a -> Bool) -> [a] -> [a]Given a predicate and a list, return the list of elements satisfying the predicate.
For example:
filter odd [1..10] = [1,3,5,7,9]any :: (a -> Bool) -> [a] -> BoolGiven a predicate and a list of elements, return True if any of the elements of the list satisfy the predicate, and False otherwise. (cf.
all)foldr :: (a -> b -> b) -> b -> [a] -> bfoldr f z xsreplaces each occurrence of(:)withfand[]withzin the listxs. That is:foldr f z [] = z
foldr f z (x:xs) = f x (foldr f z xs)
You can use this to apply an operator "between" the elements of a list, for instance:
foldr (*) 1 [5..10] == 5 * (6 * (7 * (8 * (9 * (10 * 1))))) == 151200foldr1 :: (a -> a -> a) -> [a] -> [a]Works similarly to
foldr, except that the types are more restrictive, it doesn't work on empty lists, and (this is the good part) it doesn't require the replacement for[].foldr1 f [x] = x
foldr1 f (x:xs) = f x (foldr1 f xs)
foldr1 _ [] = error "Prelude.foldr1: empty list"
max, min :: (Ord a) => a -> a -> aThese take two values of some ordered type and return the maximum and minimum of the two respectively.
From the module Char:
isUpper :: Char -> BoolDecides whether a character is uppercase. (cf.
isLower)Hope this helps.
- Cale
these are the coding that i have so far, some are incomplete you think that you could assist me with these?
question 2
fact :: Int -> Int
fact 0 = 1
fact n = n * fact (n-1)
question 6
Sum :: [Int] -> Int
Sum (x:xs)
|(xs == []) = x
|otherwise = x + sumlist xs
question 5
Isupper :: [Int] -> Bool
Isupper (x:xs)
|xs == []
|otherwise x = True
|otherwise = False
question 4
minlist :: [Int] -> Int
minlist (x:xs)
| x < minlist xs = True
| otherwise = False
question 7
getEven::[Int]->Int
getEven (x:xs)
|(mod x 2 == 0) =
|
question 3
getlarger :: Char -> char -> Char
getlarger 'a' 'b'
| 'a' > 'b' = 'a'
| otherwise = 'b'
question 1
sumAB :: Int -> Int -> Int
SumAB a b
|a==b = a
|a < b = a + sumAB (a + 1) b
Question 2: Looks good. Have you actually tried this?
Q6: Haskell function names and variables must begin with lower case. Also the names need to be consistent. Lines need to be indented, but its likely that the browser has smashed the indentation.
Q5: Lots wrong with this. Get the case right (see Q6). Get the type right (Char, not Int). Compare the structure of this with the answers to Q2 and Q6.
Q4, 7: Ditto.
Q3: Type names must begin with upper case. Stuff in quotes are string literals, not variables. Get rid of the quotes.
Q1: Why are you comparing them?
Finally, you will find Haskell tutorials at www.haskell.org.
Paul.
Ok, for the first one, I think the following is a bit more hip.
sumAB :: Int -> Int -> Int
sumAB a b = sum $ filter (not . (== 0) . (`mod` 2)) [(a+1) .. (b-1)]
Good to have seen you try though.