So I've got an Ocaml assignment and I know shit about Ocaml. Wanna help a retard with his homework? I'll probably learn faster if I see examples. So, my tasks include:
1) Write a function that flattens a list of lists
2) Write a function that will count an object's occurences in a list
3) Define a function that will create a list containing a chosen number of duplicates of a certain object
4) Define a function that will square all numbers in a list
5) Write a function that will check if a list is a palindrome
6) Define a function that will return the length of a list
7) Define a function that will append two lists
Name:
Anonymous2010-10-07 14:38
How did you get an Ocaml assignment without knowing any Ocaml? And what's stopping you from learning enough to do it? I don't know Ocaml, but it couldn't possibly take long to learn this much.
2. int count(list *car, int elem)
{
list *l = car;
int c = 0;
while(l)
{
if(l->data == elem)
c++;
l = l->next;
}
return c;
}
3. alert("``Duplicate'' doesn't meant what you think it means.")
4. (defun mapsquare (l)
"A function that will square all numbers in a list"
(if (eq () l)
()
(cons (* (car l) (car l)) (mapsquare (cdr l)))))
5. pal l | even $ len = ls == reverse (drop (len `div` 2) l)
| otherwise = ls == reverse (drop (len `div` 2 + 1) l)
where len = length l
ls = take (len `div` 2) l
6. fun {Length Xs}
case Xs of
nil then 0
[] _|Ys then 1+{Length Ys}
end
end[/code]
>>13
Hey, you started with the whole ``my posts are so good that they merit a bump'' deal.
Name:
Anonymous2010-10-07 16:07
>>14
Well, okay, maybe I was a little frustrated. Now I see it's not so hard, just made nearly the entire list. Turns out it was my compiler that was bugged, not my skill.
As I don't see how answering his questions contributed to anything other than spoiling him and increasing the number of incompetent programmers who can't even solve their own problems out of curiosity.
mapSquare =: *:
mapSquare 1 2 3 4 5
1 4 9 16 25
5'th should have elegant solution but I can't go further than isPalindrome =: (= |.) because I'm weak with hooks,forks and other kitchenware and can't say how to pass result from it to */
Name:
Anonymous2010-10-08 6:56
Most of these are built-in in CL, but that's no surprise:
;;; 1
(defun mappend (fn list
&rest otherlists
&aux (list (copy-list list)) (otherlists (copy-list otherlists)))
(apply #'mapcan fn list otherlists))
>>25
Yes, there's really a lot of flatten versions, I find mappend (non-destructive mapcan) to be the simplest, but yours is fine too. Here's another one:
(defun flatten (list)
(loop
for x in list
when (consp x)
append (flatten x)
else
append (list x)))
step the fuck back, for here comes HASKAL !!!
import Prelude hiding (length)
-- 1) Write a function that flattens a list of lists
flatten :: [[a]] -> [a]
flatten [[]] = []
flatten [x] = x
flatten (x:xs) = x ++ (flatten xs)
-- 2) Write a function that will count an object's occurences in a list
count :: (Eq a) => a -> [a] -> Int
count _ [] = 0
count x (y:ys) | x == y = 1 + (count x ys)
| otherwise = count x ys
-- 3) Define a function that will create a list containing a chosen number of duplicates of a certain object
create :: Int -> a -> [a]
create 0 _ = []
create n x = x : (create (n - 1) x)
-- 4) Define a function that will square all numbers in a list
listSquare :: (Integral a) => [a] -> [a]
listSquare = map (^2)
-- 5) Write a function that will check if a list is a palindrome
isPalindrome :: (Eq a) => [a] -> Bool
isPalindrome x = x == reverse x
-- 6) Define a function that will return the length of a list
length :: [a] -> Int
length [] = 0
length (_:xs) = 1 + length xs
-- 7) Define a function that will append two lists
append :: [a] -> [a] -> [a]
append [] ys = ys
append [x] ys = x : append [] ys
append (x:xs) ys = x : append xs ys