Transposing without recursion - list

How do I go about creating a function which will transpose a list without using recursion?
This is what I've gotten so far:
let lst = [[1;2;3];[4;5;6];[7;8;9]]
let transpLstLst (lst: 'a list list) -> 'a list list =
List.map List.head lst :: []
Which returns:
[[1;4;7]]
This is a long the lines of the output I want, but how do I go from that to being able to get to this output:
[[1;4;7];[2;5;8];[3;6;9]]

This is very inefficient*, but it meets your requirements:
let transpose (matrix : List<List<_>>) =
[
if matrix.Length > 0 then
assert(matrix |> List.distinctBy List.length |> List.length = 1)
for i = 0 to matrix.[0].Length - 1 do
yield [
for list in matrix do
yield list.[i]
]
]
Example:
let matrix = [[1; 2; 3]; [4; 5; 6]; [7; 8; 9]; [10; 11; 12]]
transpose matrix |> printfn "%A" // [[1; 4; 7; 10]; [2; 5; 8; 11]; [3; 6; 9; 12]]
* O(n3) where n is the length of a square matrix.

For better performance, you can convert the list of list to an Array2D, and then create your transposed list from the Array2D.
let transpose' xss =
let xss = array2D xss
let dimx = Array2D.length1 xss - 1
let dimy = Array2D.length2 xss - 1
[ for y=0 to dimy do [
for x=0 to dimx do
yield xss.[x,y]
]
]
For non-learning purpose, you should use the built-int List.transpose
List.transpose lst

Related

Workaround to append attaching elements in reverse order when sorting

I want to sort so that odd numbers in a list appeart first and evens appear last, but i need evens to be the same position to how they were pre sort, is there a simple workaround to this?
let rec first_odd list = match list with
| [] -> []
| h::t when h mod 2==0 -> first_odd t#[h]
| h::t -> h::first_odd t;;
first_odd[3;1;7;3;4;5;4;3;6;-1;0;3];;
first_odd[1;0;1;5;6;6;1;10;-8;4; -9];;
You can just use List.stable_sort, which implements a merge sort, with a function that compares whether or not each element is odd or even:
let first_odd =
List.stable_sort
(fun a b -> compare (a mod 2 = 0) (b mod 2 = 0))
first_odd[3;1;7;3;4;5;4;3;6;-1;0;3];;
- : int list = [3; 1; 7; 3; 5; 3; -1; 3; 4; 4; 6; 0]
first_odd[1;0;1;5;6;6;1;10;-8;4; -9];;
- : int list = [1; 1; 5; 1; -9; 0; 6; 6; 10; -8; 4]
This looks like a homework assignment, so I'll just make a few comments.
First, the expression list # [elt] has a very bad look to it. If you repeat this for n elements of a list, it has complexity of n^2, because it takes linear time to add to the end of a list. Furthermore, it's necessary to replicate the whole list to add an element to the end. So it's definitely something to avoid.
Second, you can just use List.stable_sort if you write a comparison function that gives the order you desire. This will be a lot faster than your current solution (because it will be n log n rather than n^2).
Third, if you want to work with your current method, I would keep two lists and combine them at the end.
As an academic exercise, it may help to see this implemented in terms of a fold. When using a fold, the initial state is crucial. Let's use two lists in a tuple. One for odds, and one for evens.
Each iteration we consider the initial value and the first element in the list. The function we provide uses that information to provide an updated initial value for the next iteration, which considers the next element in the list.
let list1 = [3; 1; 7; 3; 4; 5; 4; 3; 6; -1; 0; 3]
let list2 = [1; 0; 1; 5; 6; 6; 1; 10; -8; 4; -9]
let sort lst =
List.fold_left (* function *) ([], []) lst
Now, we just need a function that updates the initial value on each iteration. If the value is even, we'll tack it into the front of the evens list. Otherwise, onto the front of the odds list.
let sort lst =
List.fold_left
(fun (odds, evens) x ->
if x mod 2 = 0 then (odds, x :: evens)
else (x :: odds, evens))
([], []) lst
If we test this with list2:
utop # sort list2;;
- : int list * int list = ([-9; 1; 5; 1; 1], [4; -8; 10; 6; 6; 0])
The two lists are backwards. We can fix this with List.rev.
let sort lst =
let odds, evens =
List.fold_left
(fun (odds, evens) x ->
if x mod 2 = 0 then (odds, x :: evens)
else (x :: odds, evens))
([], []) lst
in
(List.rev odds, List.rev evens)
utop # sort list2;;
- : int list * int list = ([1; 1; 5; 1; -9], [0; 6; 6; 10; -8; 4])
Essentially we've now reinvented List.partition.
Now, we just need to concatenate those two lists.
let sort lst =
let odds, evens =
List.fold_left
(fun (odds, evens) x ->
if x mod 2 = 0 then (odds, x :: evens)
else (x :: odds, evens))
([], []) lst
in
List.rev odds # List.rev evens
utop # sort list2;;
- : int list = [1; 1; 5; 1; -9; 0; 6; 6; 10; -8; 4]

Generate all sublists of a list in OCAML [duplicate]

This question already has answers here:
Computing a set of all subsets (power set)
(2 answers)
Closed 5 years ago.
What I want to do is generating all sub-lists of a list in OCAML,
for example for
[1; 2; 3]
as a result I want to get this
[[1]; [2]; [3]; [1; 2]; [1; 3]; [2; 3]; [1; 2; 3]]
This is my approach so far:
let sublist l =
let rec aux i j max l2 =
match l with
| [] -> l
| h:t ->
if (i = max && j = max) then l2
else if (j < max ) then aux i j+1 [h]::tl
else aux i+1 i+1 l
in
aux 0 0 (List.length l) []
Use an divide and conquer approach: given a non-empty list, you get a list of all its sublists by removing its first element x and solving the problem for the remaining elements (via a recursion step).
When the recursive call returns, you combine this intermediate result with the element x you previously cut off: you have to return each of the sublist with x added in front of it and unmodified.
let rec sublists = function
| [] -> [[]]
| x::xs -> let ls = sublists xs in
List.map (fun l -> x::l) ls # ls;;
For your example input this computes:
# sublists [1;2;3];;
- : int list list = [[1; 2; 3]; [1; 2]; [1; 3]; [1]; [2; 3]; [2]; [3]; []]
The computed list can be sorted and filtered, depending on whether you want the empty list in the result or not.

Create a function that produces a list of multiples ocaml

I have a function:
let rec multiply x ls =
match ls with
[] -> []
| h::tl -> (x * h) :: multiply x tl
multiply 2 [1;2;3] = [2;4;6]
I would like a function that calls multiply from n to 0. I keep having problems because of the base case:
let rec multiply_all x ls = if x > 0
then (multiply n ls) :: multiply_all (n-1) (ls) else ????
I am not sure what to put after the else. I tried to make it
if x > 1 then (multiply n ls) :: multiply_all (n-1) (ls) else multiply all 1.
but that doesn't work.
Putting 1 there certainly doesn't work since multiply_all must return a list. So you need a list (of lists of int) to put there. But which list should it be?
The short answer is that in such simple cases, the list you need is usually the empty list: [].
As a slightly longer answer, we can consider the case for multiply_all 0 in relation to the intended results of multiply_all 1, multiply_all 2, etc., and try to find a pattern that fits. We want multiply_all to behave like this:
# multiply_all 2 [1;2;3];;
- : int list list = [[2; 4; 6]; [1; 2; 3]]
# multiply_all 1 [1;2;3];;
- : int list list = [[1; 2; 3]]
So calling multiply_all with some number N as first argument should give us a list of length N. In particular, multiply_all with N = 0 should give a list of length 0. The list of length 0 is the empty list.
Here is your completed definition:
let rec multiply_all x ls =
if x > 0 then (multiply x ls) :: multiply_all (x-1) (ls) else []
Just an other solution :
let multiply_all n l =
let multiply n= List.map (( * ) n) in
let rec aux i acc =
if i > n then acc
else aux (i+1) (multiply i l :: acc)
in
aux 1 []
;;
Test :
# multiply_all 5 [1;2;3];;
- : int list list =
[[5; 10; 15]; [4; 8; 12]; [3; 6; 9]; [2; 4; 6]; [1; 2; 3]]
First of all, your multiply method is pretty inefficient since it isn't tail recursive. Furthermore, the standard library provides you with tools to make that kind of function easier to write:
let multiply n = List.map (( * ) n);;;
val multiply : int -> int list -> int list = <fun>
multiply 5 [1;2;3];;
- : int list = [5; 10; 15]
Note: Also, use partial application when it doesn't obfuscate your code.
As of multiply_all, I'm not sure how to achieve it without JaneStreet's Core (see this question). However, here is a possible implementation using Core:
open Core.Std;; (*Using Core*)
let multiply_all n l =
let multiples = List.init n ~f:(fun x -> n-x) in (*This doesn't exist in Pervasives*)
List.map multiples ~f:(fun m -> multiply l m);;
val multiply_all : int list -> int -> int list list = <fun>
multiply_all 5 [1;2;3];;
- : int list list = [[5; 10; 15]; [4; 8; 12]; [3; 6; 9]; [2; 4; 6]; [1; 2; 3]]
Hope it helps. I'll keep this answer updated with my findings about List.init.

Sublists of list in F#

How to get sublists
[1]; [1; 2]; [1; 2; 3]; ...; [1; 2; 3; ...; n]
from list
[1; 2; 3; ...; n]
by the most idiomatic way? All that I could is:
List.scan (fun acc elem -> elem::acc) [] [1;2;3;4;5]
> val it : int list list =
[[]; [1]; [2; 1]; [3; 2; 1]; [4; 3; 2; 1]; [5; 4; 3; 2; 1]]
Thanks.
Your implementation is fine. Here is my alternative:
let source = [1..10]
let counts = [0..source.Length] // Or start at 1 if you don't want to start with an empty list
counts |> List.map (fun count -> source |> List.take count)
Throwing mine onto the pile, which is maybe a refinement of Foole's answer:
let inits list =
list |> List.mapi (fun i _ -> List.take (i + 1) list)
mapi is a useful function: You provide it a function which takes the each index and item.
Here's another one:
let f n = List.init n (fun i -> [1..(i + 1)])
List.init exists for the task of initializing a list.

F# function to find all rotations of a list

I have some F# code here for a recursive function that rotates a list to the left by n places. I am very new to F# and I'm looking for a way to modify this code to output not just one rotation by n positions, but all possible rotations.
For example, say I have the list:
let list1 = [1; 2; 3; 4]
I want to call rotate on this list such that the output is:
[ [1; 2; 3; 4]; [2; 3; 4; 1]; [3; 4; 1; 2]; [4; 1; 2; 3] ]
The code I have that does a left shift by n is:
let rec rotate xs k =
match xs, k with
|[], _ -> []
|xs, 0 -> xs
|x::xs, k when k > 0 -> rotate(xs # [x])(k-1)
|xs, k -> rotate xs (List.length xs + k)
I'm not sure how to edit this to do the steps listed above. Any help or resources would be appreciated. I should add that I really want to function to be recursive. Thanks.
If I understand the question correctly, you can also write the function using the built-in List.permute function:
let rotate xs =
let length = xs |> List.length
let perm n = xs |> List.permute (fun index -> (index + n) % length)
[1 .. length] |> List.rev |> List.map perm
Example output (slightly formatted for improved readability):
> [1 .. 4] |> rotate;;
val it : int list list =
[[1; 2; 3; 4];
[2; 3; 4; 1];
[3; 4; 1; 2];
[4; 1; 2; 3]]
I would start off with making an infinite cyclic sequence off your original list. And then use List.init to get all the rotations.
let rotations list =
let rec cyclic sequence = seq {
yield! sequence
yield! cyclic sequence }
let cyclic = cyclic list
let length = List.length list
List.init length (fun i -> cyclic |> Seq.skip i |> Seq.take length |> List.ofSeq)
the important thing is, that the sequence is lazy and therefore can be infinite.
Using the rotate function you already wrote:
let rotations xs = List.init (List.length xs) (rotate xs)
By the way, you can shorten your rotate function to this:
let rec rotate xs k =
match xs, k with
|[], _ -> []
|xs, 0 -> xs
|x::xs, k -> rotate (xs # [x]) (k-1)
Patterns are matched top-down, so the guard when k > 0 is not necessary. The last line of your original solution would never match, so I removed it.
Since I wanted to do this specific question using recursion and matchings, I managed to figure it out and this is what I came up with:
let rotate xs =
let n = List.length xs
let rec rotation xs n =
match xs, n with
|[], _ -> []
|xs, 0 -> xs
|x::xs, n -> rotation (xs # [x]) (n-1)
let rec rotateList xs n = //we are compiling a list of different rotations
match xs, n with
|xs, 0 -> []
|xs, n -> (rotation xs ((List.length xs)-n))::rotateList xs (n-1)
rotateList xs n
I also specifically wanted only one input parameter, namely the list