Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Ocaml

Name: Anonymous 2010-10-07 14:26

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: Anonymous 2010-10-07 15:16

1.
flatten([],[]), !.
flatten([[]|Xs],Xs), !.
flatten([X|Xs],[X|Ys]) :- atomic(X), flatten(Xs,Ys), !.
flatten([X|Xs],Zs) :- flatten(X,Y),
                      flatten(Xs,Ys),
                      append(Y,Ys,Zs).


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]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List