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

Pages: 1-

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 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.

Name: Anonymous 2010-10-07 14:40

Kill yourself.

Name: Anonymous 2010-10-07 14:51

If you can't learn to do this shit on your own, you are going to be a burden to everyone else you work with.  Forever.

Get it done, or >>3

Name: Anonymous 2010-10-07 14:57

>>4
You don't understand. If I get through the first few examples, I will learn it much faster.

Name: Anonymous 2010-10-07 15:01

You will learn much faster by doing it yourself.

Name: Anonymous 2010-10-07 15:07

>>6
Yeah, because you know every person on the planet.

Name: Anonymous 2010-10-07 15:16

>>7
No, it is because you touch yourself at night.

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]

Name: Anonymous 2010-10-07 15:17

>>7
You're not going to get help with that attitude, greenname.

Name: Anonymous 2010-10-07 15:31

wow, you are a retard.

Name: Anonymous 2010-10-07 15:45

>>9
You forgot 7.
APPEND←, ⍝ APPEND TWO LISTS

Name: Anonymous 2010-10-07 15:52

Wow, 4chan is surely full of douchebags.

Name: Anonymous 2010-10-07 15:54

>>13
Hey, you started with the whole ``my posts are so good that they merit a bump'' deal.

Name: Anonymous 2010-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.

Name: Anonymous 2010-10-07 16:12

>>15
This post was not bump-worthy either.

Name: Anonymous 2010-10-07 16:18

lol fuck you fag

Name: Anonymous 2010-10-07 16:19

>>13
I don't see your problem. >>9 solved all but one of the exercises.

Name: Anonymous 2010-10-07 17:01

/prog/ is now  /kindergarten/

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.

Name: Anonymous 2010-10-07 17:29

>>19
lol'd at answering

bumping just to troll you

Name: Anonymous 2010-10-07 17:30

>>20
lmfao

Name: Anonymous 2010-10-08 5:12

u maddddddddd

Name: Anonymous 2010-10-08 6:08

J is so superiour for this shit


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: Anonymous 2010-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))

(defun flatten (list)
  (mappend #'(lambda (e) (if (consp e) (flatten e) (list e))) list))
;CL-USER> (flatten '(1 (2 3 4) (5 (6 (7 8))) 9))
;(1 2 3 4 5 6 7 8 9)

;;; 2
#'count
;CL-USER> (count 1 '(1 2 3 1 0 1))
;3
;;; 3
#'make-list
;CL-USER> (make-list 5 :initial-element 'hi)
;(HI HI HI HI HI)
;;; 4
(defun square (x)
  (* x x))

(defun square-list (list)
  (mapcar #'square list))

;CL-USER> (square-list '(1 2 3))
;(1 4 9)

;;; 5
(defun palindromep (list)
  (equal list (reverse list)))

;CL-USER> (palindromep '(1 2 3 2 1))
;T
;CL-USER> (palindromep '(1 2 3 2))
;NIL

;;; 6
#'length

;CL-USER> (length '(a b c))
;3

;;; 7
#'append

;CL-USER> (append (list 1 2 3) '(a b c))
;(1 2 3 A B C)

However, if they weren't built-in, it's pretty easy to implement them.

Name: Anonymous 2010-10-08 10:09

>>24

This seems to be a simpler solution to #1:


(defun flatten (tree)
  (cond ((null tree) nil)
        ((consp tree)
         (append (flatten (car tree))
                 (flatten (cdr tree))))
        (t (list tree))))

Name: Anonymous 2010-10-08 10:12

>>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)))

Name: Anonymous 2010-10-08 10:30


(defun flatten (list)
  (loop for x in list append (if (consp x) (flatten x) (list x))))

Name: Anonymous 2010-10-08 13:12

(define (flatten l)
  (letrec ((g (lambda (l s r)
                (if (null? l)
                    (if (null? s)
                           (r (list))
                           (g (car s) (cdr s) r))
                    (if (list? (car l))
                        (g (car l) (cons (cdr l) s) r)
                        (g (cdr l) s (lambda (k) (r (cons (car l) k)))))))))
    (g l '() (lambda (l) l))))
(flatten '((a b c) ((d e) (f g) h ) i (j (k (l (m n) o)) p)))

Name: Anonymous 2010-10-08 13:15

Actually, I can go one better
(define (flatten l)
 (((lambda (l)
    ((lambda (l) (l l))
     (lambda (r)
       (l (lambda s (apply (r r) s))))))
   (lambda (g) (lambda (l s r)
            (if (null? l)
                (if (null? s)
                    (r (list))
                    (g (car s) (cdr s) r))
                (if (list? (car l))
                    (g (car l) (cons (cdr l) s) r)
                    (g (cdr l) s (lambda (k) (r (cons (car l) k)))))))))
  l (list) (lambda (l) l)))

Name: Anonymous 2010-10-08 14:16

>>25
That's basically the one in SICP.

Name: Anonymous 2010-10-08 15:07

>>23
,       NB. monadic
(+/@:=) NB. dyadic
#       NB. dyadic
*:      NB. monadic
(-:|.)  NB. monadic
$       NB. monadic
,       NB. dyadig

Name: Anonymous 2010-10-08 16:39

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

Name: Anonymous 2010-10-08 18:29

>>32

lrn2higher order functions.


-- 1)
concat = foldl (++) []

-- 2)
count x = length . filter (== x)

Name: Anonymous 2010-10-08 18:30

>>33
where?

Name: Anonymous 2010-10-08 21:28

>>32
Isn't flatten [[]] = [] just a special case of flatten [x] = x?

Name: Anonymous 2010-10-09 7:12

>>35
so, how do you flatten "HAX MY ANUS", which really is just ['H','A','X',' ','M','Y',' ','A','N','U','S']?

Name: Anonymous 2010-10-09 13:06

FLATTEN MY ANUS

Name: Anonymous 2010-10-09 15:12

>>36-37
Go away.

Name: Anonymous 2010-10-10 7:16

its pronunced i h4x my 4nu5

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