let rec f (l: int list) : int * int =
begin match l with
| [] -> (0,0)
| [x] (x,x)
| x::y::tl -> let (a,b) = f tl in
(x + a, y + b)
end
let r = f [2;3;4;5;6]
I'm thinking that the answer would compute to r = (6,6) because if you keep calling the recursive function on the tail, you'll end up with the last item in the list, thus it will match to the second case but I have no idea if this is correct or why this is the case. Really, I don't understand the use of let (a,b) = f tl in (x + a, y + b).
I'll try to explain let (a, b) = f tl in (x + a, y + b). Maybe that will help.
The pattern for this case is x :: y :: tl. So this means that tl represents the tail of the tail of the list. I.e. it represents the list after you remove the two elements at the front.
Along these same lines, x represents the first element of the list and y represents the second element.
The recursive call to f is going to return whatever f returns for tl, which is what you're trying to figure out. This will be a pair of numbers, as you can tell from the pattern (a, b).
The result of the whole expression (which is the result of f except when the list is very short) is (x + a, y + b). In other words, it adds the first element of the list to the first number of the pair and the second element of the list to the second element of the pair.
Related
For my homework assignment in ML I have to use the fold function and an anonymous function to turn a list of integers into the alternating sum. If the list is empty, the result is 0. This is what I have so far. I think what I have is correct, but my biggest problem is I cannot figure out how to write what I have as an anonymous function. Any help would be greatly appreciated.
fun foldl f y nil = y
| foldl f y (x::xr) =
foldl f(f(x,y))xr;
val sum = foldl (op -) ~6[1,2,3,4,5,6];
val sum = foldl (op -) ~4[1,2,3,4];
val sum = foldl (op -) ~2[1,2];
These are just some examples that I tested to see if what I had worked and I think all three are correct.
There are two cases: one when the list length is even and one when the list length is odd. If we have a list [a,b,c,d,e] then the alternating sum is a - b + c - d + e. You can re-write this as
e - (d - (c - (b - a)))
If the list has an even length, for example [a,b,c,d] then we can write its alternating sum as
- (d - (c - (b - a))).
So to address these two cases, we can have our accumulator for fold be a 3-tuple, where the first entry is the correct value if the list is odd, the second entry is the correct value if the list is even, and the third value tells us the number of elements we've looked at, which we can use to know at the end if the answer is the first or second entry.
So an anonymous function like
fn (x,y,n) => (x - #1 y, ~(x + #2 y), n + 1)
will work, and we can use it with foldl with a starting accumulator of (0,0,0), so
fun alternating_sum xs =
let
(v1, v2, n) = foldl (fn (x,y,n) => (x - #1 y, ~(x + #2 y), n + 1)) (0,0,0) xs
in
if n mod 2 = 0 then v2 else v1
end
I'm looking for a functional way to implement this:
list = [a b c d e f]
foo(list, 3) = [[a d] [b e] [c f]]
A potential solution is:
foo(list,spacing) = zip(goo(list,spacing))
Where, for example,
goo([a b c d e f],3) = [[a b c] [d e f]]
What is foo and goo usually called, so I can look for existing solutions rather than reinventing the wheel?
Notes: Rather than trying to explain with words, I've just shown examples that'll be hopefully much easier to get. Arbitrary syntax for broader understanding.
You can use partition:
(partition 3 '[a b c d e f])
=> ((a b c) (d e f))
(partition 2 '[a b c d e f])
=> ((a b) (c d) (e f))
Edit:
(apply map list (partition 3 '[a b c d e f]))
=> ((a d) (b e) (c f))
I do not think there is a built-in function for that. It's easy and nice to implement.
I know you do not want the implementation, but one of the tags was Haskell so maybe you want to see this
p :: Int -> [a] -> [[a]]
p n xs = [ [x | (x ,y) <- ys , y `mod` n == i] | i <- [0 .. n - 1] , let ys = zip xs [0 .. ]]
That is pretty functional.
Your goo function is drop with flipped arguments. Given that, you can implement foo almost like you say in your question:
let foo list spacing = zip list (drop spacing list)
This still doesn't exactly give the result you need though, but close:
Prelude> foo "abcdef" 3
[('a','d'),('b','e'),('c','f')]
EDIT:
Reading more carefully, your goo function is splitAt with flipped arguments. Given that, foo can be defined like this:
let foo list spacing = (uncurry zip) $ splitAt spacing list
Which is the same as:
let foo list spacing = let (left, right) = splitAt spacing list
in zip left right
Eventually what I want is what x represents:
let x = (something, (myfunc1 para1));;
so that when calling x, I get a tuple, but when calling (snd x) para, I will get a return value of myfunc1 para.
What I'm trying is like this:
let myfunc2 para1 para2 =
let myfunc1 para2 = ... in
( (fst para1), (myfunc1 para2) );;
And I want to call myfunc2 like this:
let x = myfunc2 para1 to get what I described above. However, what I get is just a function which when called with para1 will return a regular tuple, not a (something, function) tuple
You have a useless para2 parameter in your definition. The correct way is:
let myfunc2 para1 =
let x = ... in
let myfunc1 para2 = ... in
( x, myfunc1 );;
But it would help if we could speak about a concrete example. You are misunderstanding something obvious, but I do not know what.
Here is a concrete example. Suppose we want a function f which accepts a number n and returns a pair (m, g) where m is the square of n and g is a function which adds n to its argument:
let f n =
let m = n * n in
let g k = n + k in
(m, g)
Or shorter:
let f n = (n * n, fun k => n + k)
Now to use this, we can do:
let x = f 10 ;;
fst x ;; (* gives 100 *)
snd x ;; (* gives <fun> *)
snd x 5 ;; (* gives 15, and is the same thing as (snd x) 5 *)
Now let us consider the following bad solution in which we make the kind of mistake you have made:
let f_bad n k =
let m = n * n in
let g k = n + k in
(m, g k)
Now f_bad wants two arguments. If we give it just one, we will not get a pair but a function expecting the other argument. And when we give it that argument, it will return a pair of two integers because (m, g k) means "make a pair whose first component is the integer m and the second component is g applied to k, so that is an integer, too."
Another point worth making is that you are confusing yourself by calling two different things para2. In our definition of f_bad we also confuse ourselves by calling two different things k. The k appearing in the definition of g is not the same as the other k. It is better to call the two k's different things:
let f_bad n k1 =
let m = n * n in
let g k2 = n + k2 in
(m, g k1)
Now, does that help clear up the confusion?
Say we have a list of coordinates like:
(1,2)
(0,3)
(4,1)
(0,3)
(-2,3)
(6,5)
And we wanted to result in the following list, which is defined as the summation of each consecutive coordinates. (Sorry bad definition) like so:
(1,5)
(4,4)
(4,4)
(-2,6)
(4,8)
So there exists a set A = (a,b,c,...,n) where a,b,c,...,n are coordinates in R^2.
There exists a function f such that f(A) = B = (a+b,b+c,c+d,...,n-1+n).
~
How would you write something like that in a functional language like Haskell? A program that applies f to a given A to give B.
You can use zip to zip the list with its tail, you get pairs of pairs like [((1,2), (0,3)), ((0,3),(4,1)), ...]. Then you can use map to replace each pair of pairs with its sum. Or you can use zipWith which is basically zip + map in one function, except the function given to zipWith has type a -> b -> c, not (a,b) -> c:
summedCoords = zipWith (\ (a,b) (c,d) -> (a+c, b+d)) coords (tail coords)
You can write a generic function like this
g:: (a -> a -> b) -> [a] -> [b]
g f (x1:x2:xs) = (f x1 x2):(g (x2:xs))
g _ (x1:[]) = []
and pass it your add function
f = g f' where
f' (a,b) (a',b') = (a+a', b+b')
I have two functions f and g and I am trying to return f(g(x)) but I do not know the value of x and I am not really sure how to go about this.
A more concrete example: if I have functions f = x + 1 and g = x * 2 and I am trying to return f(g(x)) I should get a function equal to (x*2) + 1
It looks like you have it right, f(g(x)) should work fine. I'm not sure why you have a return keyword there (it's not a keyword in ocaml). Here is a correct version,
let compose f g x = f (g x)
The type definition for this is,
val compose : ('b -> 'c) -> ('a -> 'b) -> 'a -> 'c = <fun>
Each, 'a,'b,'c are abstract types; we don't care what they are, they just need to be consistent in the definition (so, the domain of g must be in the range of f).
let x_plus_x_plus_1 = compose (fun x -> x + 1) (fun x -> x * 2)