Skip to content

Instantly share code, notes, and snippets.

@camilopayan
Created March 9, 2010 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camilopayan/327036 to your computer and use it in GitHub Desktop.
Save camilopayan/327036 to your computer and use it in GitHub Desktop.
let rec inner xs ys = match (xs,ys) with
| (x::xs,y::ys) -> (x*y) + inner xs ys
| _ -> 0
let rec separate = function
| [] -> ([],[])
| []::xs -> ([],[])
| (x::xs)::ys -> let (M,N) = separate ys
(x::M, xs::N)
let rec transpose = function
| [] -> []
| x::xs -> let (q, w) = separate (x::xs) in
if List.length(q) = 0 then [] else [q] @ trans w
(*Hooray! Slightly more efficient than it was before! Now it'll only transpose that thing once*)
let rec multiply = function
| ([],ys) -> []
| (x::xs,ys) when (List.length (x)) = (List.length ys) ->
multiply(x::xs,transpose ys)
| (x::xs,ys) -> List.map (inner x) ys :: multiply(xs,ys)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment