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

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