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

Submitted by itkovian on Fri, 07/29/2005 - 2:29pm.

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.

Submitted by Cale Gibbard on Sat, 07/30/2005 - 11:27am.

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] -> Bool
Given 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] -> b
foldr f z xs replaces each occurrence of (:) with f and [] with z in the list xs. 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))))) == 151200

foldr1 :: (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 -> a
These take two values of some ordered type and return the maximum and minimum of the two respectively.

From the module Char:
isUpper :: Char -> Bool
Decides whether a character is uppercase. (cf. isLower)

Hope this helps.
- Cale

Submitted by babyneri on Wed, 08/10/2005 - 8:34am.

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

Submitted by Paul Johnson on Tue, 08/16/2005 - 11:53pm.

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.

Submitted by itkovian on Thu, 09/01/2005 - 4:59am.

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.