I have this snippet of the code in ML:
local
fun unfolder( [] , n ) = []
| unfolder( l::ls, n ) = (n, l) :: unfolder( ls, n )
in
fun flat list = unfolder(list, 1)
end;
it gives me an error:
unexpected exception (bug?) in SML/NJ: EA [EA]
raised at: ../../MLRISC/x86/mltree/x86.sml:417.32-417.34
../compiler/Basics/stats/stats.sml:198.40
../compiler/Basics/stats/stats.sml:198.40
../compiler/Basics/stats/stats.sml:198.40
../compiler/TopLevel/interact/evalloop.sml:44.55
but when I change its (n, l) to (n, l:int) it works, and when to (n, l:'a), can somebody please explain why polymorphic type doesn't work, thanks in advance
It is an internal bug in SML/NJ. The program works flawlessly if compiled with MLton, adding:
val _ =
let val l = flat [1,2,3]
fun printer (a,b) = Int.toString(a) ^ ", " ^ Int.toString(b)
in
print (printer (hd l) ^ "\n")
end
Report it. Your example seem digestible in size - and probably has to do with polymorphism representation.
Related
I want to write the following procedure using pattern matching:
fun sub1 ns = if null ns then []
else (hd ns) :: (sub1 (tl ns));
Where null, hd, and tl are:
fun null [] = true
| null (_::_) = false;
fun hd (x::_) = x;
fun tl (_::xs) = xs;
So far I have tried this:
fun sub1.2 [] = []
| sub1.2 (n::ns) = n :: sub1.2 ns;
The above does not work. I get the following syntax errors:
2.1.sml:6.10 Error: syntax error: inserting ASTERISK
2.1.sml:7.10 Error: syntax error: inserting ASTERISK
2.1.sml:7.33 Error: syntax error: inserting EQUALOP
So again, how do you write sub1 using pattern matching? Thanks!
Decimal points aren't allowed in names; you'll have to remove or replace them.
Since you managed to find a solution, try and see if you can rewrite the following using pattern matching:
fun merge (xs, ys) =
if null xs orelse null ys
then []
else (hd xs, hd ys) :: merge (tl xs, tl ys)
val example = merge ([1,2,3], [4,5,6]) (* [(1,4), (2,5), (3,6)] *)
You may need more than one base case, but they'd probably look very similar.
I'm used to JaneStreet's Core library. Its List module has a neat init function:
List.init;;
- : int -> f:(int -> 'a) -> 'a list = <fun>
It allows you to create a list with using a custom function to initialize elements:
List.init 5 ~f:(Fn.id);;
- : int list = [0; 1; 2; 3; 4]
List.init 5 ~f:(Int.to_string);;
- : string list = ["0"; "1"; "2"; "3"; "4"]
However, this function doesn't seem to exist in Pervasives, which is sad. Am I missing something, or do I have to implement it myself? And if I do need to write it, how do I achieve this?
EDIT:
I have written an imperative version of init, but it doesn't feel right to have to resort to OCaml's imperative features in such a case. :(
let init n ~f =
let i = ref 0 in
let l = ref [] in
while !i < n do
l := (f !i) :: !l;
incr i;
done;
List.rev !l
;;
EDIT 2:
I've opened a pull request on OCaml's GitHub to have this feature included.
EDIT 3:
The feature was released in OCaml 4.06.
A recursive implementation is fairly straightforward. However, it is not tail-recursive, which means that you'll risk a stack overflow for large lists:
let init_list n ~f =
let rec init_list' i n f =
if i >= n then []
else (f i) :: (init_list' (i+1) n f)
in init_list' 0 n f
We can transform it into a tail-recursive version using the usual techniques:
let init_list n ~f =
let rec init_list' acc i n f =
if i >= n then acc
else init_list' ((f i) :: acc) (i+1) n f
in List.rev (init_list' [] 0 n f)
This uses an accumulator and also needs to reverse the intermediate result, as the list is constructed in reverse. Note that we could also use f (n-i-1) instead of f i to avoid reversing the list, but this may lead to unexpected behavior if f has side-effects.
An alternative and shorter solution is to simply use Array.init as a starting point:
let init_list n ~f = Array.(init n f |> to_list)
You can copy the code from JaneStreet and use it.
The code look's like (but not exactly the same) :
let init n ~f =
if n < 0 then raise (Invalid_argument "init");
let rec loop i accum =
if i = 0 then accum
else loop (i-1) (f (i-1) :: accum)
in
loop n []
;;
You can find the original code inside core_list0.ml from the package core_kernel.
I'm having a problem with understanding how F# works. I come from C# and I think that I'm trying to make F# work like C#. My biggest problem is returning values in the correct format.
Example:
Let's say I have function that takes a list of integers and an integer.
Function should print a list of indexes where values from list match passed integer.
My code:
let indeks myList n = myList |> List.mapi (fun i x -> if x=n then i else 0);;
indeks [0..4] 3;;
However it returns:
val it : int list = [0; 0; 0; 3; 0]
instead of just [3] as I cannot ommit else in that statement.
Also I have targeted signature of -> int list -> int -> int list and I get something else.
Same goes for problem no. 2 where I want to provide an integer and print every number from 0 to this integer n times (where n is the iterated value):
example:
MultiplyValues 3;;
output: [1;2;2;3;3;3]
Best I could do was to create list of lists.
What am I missing when returning elements?
How do I add nothing to the return
example: if x=n then n else AddNothingToTheReturn
Use List.choose:
let indeks lst n =
lst
|> List.mapi (fun i s -> if s = n then Some i else None)
|> List.choose id
Sorry, I didn't notice that you had a second problem too. For that you can use List.collect:
let f (n : int) : list<int> =
[1 .. n]
|> List.collect (fun s -> List.init s (fun t -> s))
printfn "%A" (f 3) // [1; 2; 2; 3; 3; 3]
Please read the documentation for List.collect for more information.
EDIT
Following s952163's lead, here is another version of the first solution without the Option type:
let indeks (lst : list<int>) (n : int) : list<int> =
lst
|> List.fold (fun (s, t) u -> s + 1, (if u = n then (s :: t) else t)) (0, [])
|> (snd >> List.rev)
This one traverses the original list once, and the (potentially much shorter) newly formed list once.
The previous answer is quite idiomatic. Here's one solution that avoids the use of Option types and id:
let indeks2 lst n =
lst
|> List.mapi (fun i x -> (i,x))
|> List.filter (fun x -> (fst x) % n = 0 )
|> List.map snd
You can modify the filter function to match your needs.
If you plan to generate lots of sequences it might be a good idea to explore Sequence (list) comprehensions:
[for i in 1..10 do
yield! List.replicate i i]
If statements are an expression in F# and they return a value. In this case both the IF and ELSE branch must return the same type of value. Using Some/None (Option type) gets around this. There are some cases where you can get away with just using If.
I just start working with standard ml and really have some trouble understanding the list in this language. So my question is how to shorten a list in ml ? For example, if I have a list [1,2,3,4,5,6], I want to shorten it to [1,2]. What I'm having so far is:
fun shorten(i, l) = let val newlen = i in newlen = length l//in correct
what I want is a function that will take i as a location that user want to shorten the list and l is the list. In this case, the input should look like shorten(2, [1,2,3,4,5,6] and the output should look like [1,2]
This function should do it:
fun shorten(_, nil) = nil
| shorten(0, _) = nil
| shorten(i, x::xs) = x::shorten(i - 1, xs)
As you noticed, this function doesn't throw any exceptions when i is larger than the length of the list. An approach that uses exceptions would be this:
exception IllegalArgument
fun shorten(_, nil) = nil
| shorten(0, _) = nil
| shorten(i, x::xs) =
if i > length(x::xs) then raise IllegalArgument
else x::shorten(i - 1, xs)
In SML you need to declare any exception type with exception before it is raised using raise. Otherwise, the exception type is not bound in the environment and the interpreter will complain about the symbol being unknown.
The SML Basis Library contains the function List.take to perform the required task as part of the List Structure.
- fun shorten ( toHowMany, myList ) = List.take ( myList, toHowMany ) ;
val shorten = fn : int * 'a list -> 'a list
- shorten ( 2, [1,2,3,4,5,6] ) ;
val it = [1,2] : int list
If the order of the arguments doesn't matter, then List.take can be used directly:
- List.take ( [1,2,3,4], 2 ) ;
val it = [1,2] : int list
i'm trying to learn ocaml right now and wanted to start with a little program, generating all bit-combinations:
["0","0","0"]
["0","0","1"]
["0","1","0"]
... and so on
My idea is the following code:
let rec bitstr length list =
if length = 0 then
list
else begin
bitstr (length-1)("0"::list);
bitstr (length-1)("1"::list);
end;;
But i get the following error:
Warning S: this expression should have type unit.
val bitstr : int -> string list -> string list = <fun>
# bitstr 3 [];;
- : string list = ["1"; "1"; "1"]
I did not understand what to change, can you help me?
Best regards
Philipp
begin foo; bar end executes foo and throws the result away, then it executes bar. Since this makes only sense if foo has side-effects and no meaningful return value ocaml emits a warning if foo has a return value other than unit, since everything else is likely to be a programmer error (i.e. the programmer does not actually intend for the result to be discarded) - as is the case here.
In this case it really does make no sense to calculate the list with "0" and then throw it away. Presumably you want to concatenate the two lists instead. You can do this using the # operator:
let rec bitstr length list =
if length = 0 then
[list]
else
bitstr (length-1)("0"::list) # bitstr (length-1)("1"::list);;
Note that I also made the length = 0 case return [list] instead of just list so the result is a list of lists instead of a flat list.
Although sepp2k's answer is spot on, I would like to add the following alternative (which doesn't match the signature you proposed, but actually does what you want) :
let rec bitstr = function
0 -> [[]]
| n -> let f e = List.map (fun x -> e :: x) and l = bitstr (n-1) in
(f "0" l)#(f "1" l);;
The first difference is that you do not need to pass an empty list to call the function bitsr 2 returns [["0"; "0"]; ["0"; "1"]; ["1"; "0"]; ["1"; "1"]]. Second, it returns a list of ordered binary values. But more importantly, in my opinion, it is closer to the spirit of ocaml.
I like to get other ideas!
So here it is...
let rec gen_x acc e1 e2 n = match n with
| 0 -> acc
| n -> (
let l = List.map (fun x -> e1 :: x) acc in
let r = List.map (fun x -> e2 :: x) acc in
gen_x (l # r) e1 e2 (n - 1)
);;
let rec gen_string = gen_x [[]] "0" "1"
let rec gen_int = gen_x [[]] 0 1
gen_string 2
gen_int 2
Result:
[["0"; "0"]; ["0"; "1"]; ["1"; "0"]; ["1"; "1"]]
[[0; 0]; [0; 1]; [1; 0]; [1; 1]]