I have a list L = [1,2,3].
I perform the following on L:
lists:sublist(L,2) ++ [10] ++ lists:nthtail(3,L).
Instead of storing the result in a new list, I want to store the result in L itself. However, when I do so, I am getting the obvious error: ** exception error: no match of right hand side value [1,2,300]
I don't want to use a new variable, I want to rewrite in L itself. Is it possible?
No, Erlang has single assignment. To use an example from Armstrong, in C this works:
x = 5;
x = x + 10;
But in Erlang it is written:
X = 5;
X1 = X + 10;
Related
So I have this function where i'm taking in two float lists and using their respective index elements to calculate this simple formula: (x-y)^2 / x for every index (0,1,2,3...)
Here is what I have so far:
let myCalc (list1: float list) (list2: float list) : float list =
List.map2 (fun x y -> (x-y)^2 / x) list1 list2
I keep getting this error: This expression was expected to have type 'float' but here has type 'string'
How come my approach listed above won't work but this example does:
let list1 = [1; 2; 3]
let list2 = [4; 5; 6]
let sumList = List.map2 (fun x y -> x + y) list1 list2
printfn "%A" sumList
Can someone explain how I can understand the difference between the code that I've written and the example code listed above? And yes, I already tried setting the List.map2 to a variable and then printing it out but that didn't work either. I think it has something to do with the way I'm doing my calculations, I just don't know what is wrong.
Also, I want my output result to be stored in a list of the respective x and y indexes. Please help.
The reason for this error is relatively simple, you are using the wrong power operator (^). In F #, and in the family of ML languages in general, this operator is a concatenation string operator. This is why you get this error, since the compiler expects to find two parameters of the string type in rvalue and lvalue for the^operator.
You must use the ** operator:
let myCalc (list1: float list) (list2: float list) : float list =
List.map2 (fun x y -> (x - y) ** 2. / x) list1 list2
2 is a literal of typeint, we have to specify that we want to use a literal value of type float for the expression, so this gives us this:2.
Context:
I am trying to define a function in ocaml that inserts an element x in a list, at either the head or the tail of the list, based off of whether the new element is less than the current head of the list.
The Problem:
The problem is that when I run some code that I created (shown at bottom), my list is going back to its original state and not saving the append that was previously done. I realize that I can just do this with an easy let statement of a new variable or the same, but I would like to just save the new list as its current form. Can this even be done in ocaml without the creation of a new list?
My question is: How can I append to a list, and save it in its new form, without the creating of a new list or variable.
Research:
I was looking at this answer on SO, and have incorporated it in my code already. However, when I run this code:
let rec insertion x y =
match y with
| [] -> x::y
| h::tl -> if h >= x then x::y
else y#[x]
;;
, accompanied by:
let y = [1;2;3];;
insertion 0 y ;;
y ;;
I return:
val y : int list = [1; 2; 3]
- : int list = [0; 1; 2; 3]
- : int list = [1; 2; 3]
It is impossible. OCaml's list is immutable, you can't change it. You can't change its values, you can't change its length.
I would like to understand how sequential composition works much better than I do now in SML. I have to write a program that takes a list of integers and moves the integer at index zero to the last index in the list. ie. [4, 5, 6] -> [5, 6, 4].
The code I have right now is:
- fun cycle3 x =
= if length(x) = 1 then x
= else (List.drop(x, 1);
= x # [hd(x)]);
val cycle3 = fn : 'a list -> 'a list
The question lies in my else statement, what I want to happen is first concatenate the first term to the end, and then second drop the first term. It seems simple enough, I just don't understand how to perform multiple functions in a particular order using SML. My understanding was that the first function called has the scope of the second function that would have the scope of the third function.. etc etc.. What am I doing wrong here?
Most things in SML are immutable -- your function, rather than modifying the list, is building a new list. List.drop(x,1) evaluates to a new list consisting of all but the first element of x, but does not modify x.
To use your method, you would bind the result of List.drop(x,1) to a variable, as in the following:
fun cycle3 x = if length x = 1
then x
else let
val y = List.drop(x,1)
in
y # [hd(x)]
end
Alternately, a cleaner way of doing this same thing, that also handles the possibility of an empty list:
fun cycle3 [] = []
| cycle3 (x::xs) = xs # [x]
I am new to OCaml. I am trying to use List.nth just like List.length but it keeps giving me a syntax error or complains about not matching the interface defined in another file. Everything seems to work fine if I comment out using List.nth
Thanks
It's hard to help unless you show the code that's not working. Here is a session that uses List.nth:
$ ocaml
OCaml version 4.00.0
# let x = [3;5;7;9];;
val x : int list = [3; 5; 7; 9]
# List.nth x 2;;
- : int = 7
#
Here's a session that defines a function that uses List.nth. (There's nothing special about this.)
# let name_of_day k =
List.nth ["Mon";"Tue";"Wed";"Thu";"Fri";"Sat";"Sun"] k;;
val name_of_day : int -> string = <fun>
# name_of_day 3;;
- : string = "Thu"
#
(As a side comment: using List.nth is often inappropriate. It takes time proportional to n to find the nth element of a list. People just starting with OCaml often think of it like accessing an array--i.e., constant time--but it's not.)
fun temp(x) =
let val a = x
in if a mod (x-1) = 0 then x
else temp(x-1)
end;
this is example Standard ML code.
What I want to know is that
for example user call temp(10); then a = 10 and the temp(x-1) is called.
then the variable 'a' will change to 9. Can I keep it as 10 in Standard ML?
If your function contains val a = x, then for any invocation of temp(x), the value of a will be equal to the value of x. If you want to remember the value of a from a previous invocation when you recurse, you need to pass it around as a parameter like this:
fun temp_helper x a =
if a mod (x-1) = 0 then x
else temp_helper (x-1)
fun temp x = temp_helper x x
You could also make the helper function an inner function. If you do that, you can actually remove a as a parameter and instead close over a like this:
fun temp x =
let
val a = x
fun helper x =
if a mod (x-1) = 0 then x
else helper (x-1)
in
helper x
end