I need a function where it gets a list as an input and it returns each element of that list. But if the list is empty it should return nothing. can someone help me? i want to done using recursion and cps. but i do not know how to use cps. I am new to ocaml.
Hello. I did not understand what you are required to do, but here is an example of implementation of List.map
using CPS:
let rec map_cps f l k =
match l with
| [] -> k []
| x :: xs -> map_cps f xs (fun t -> k (f x :: t))
let map f l = map_cps f l Fun.id;;
Here is an example of output:
map (fun a -> a + 1) [1; 2; 3; 4; 5];;
- : int list = [2; 3; 4; 5; 6]
Hope it can help!
Thank you for helping me.
I need a function like to get this result.
choose (fun thing → Printf.printf “%i” thing) [] ;;
( prints nothing)
choose (fun thing → Printf.printf "%i " thing) [1] ;;
( prints: 1 )
choose (fun thing → Printf.printf "%i " thing) [0; 1; 2] ;;
( prints: 0 1 2)
function name is => choose x list
hope you understand and thak you again. please help
choose etc things <== this is the function name
Call the continuation etc on each member of the list things.<== this is what should it do
these are the results from this function
choose (fun thing → Printf.printf “%i” thing) [] ;;
( prints nothing)
choose (fun thing → Printf.printf "%i " thing) [1] ;;
( prints: 1 )
choose (fun thing → Printf.printf "%i " thing) [0; 1; 2] ;;
( prints: 0 1 2)
please help. Thank you
If you rename the function @cloudyhug wrote for you and supply your printing lambda instead of the incrementing they used as an example then you’ll get the expected output.
@Mahela_Pradeep: as a discussion forum we don’t want to start giving full solutions to assignments, otherwise we would be flooded with people asking us to do their homework, which is not very interesting to anyone. If you want to ask questions about a given problem, please start by explaining what work you have done on this problem: try to solve it and show a first attempt at an answer. (If you cannot even write a first attempt, you need to go back at your course material, not a discussion forum.)
(Also: please avoid opening several topics for all your questions, let’s start with one question and see how that goes.)
Others: we haven’t had a collective discussion on how to deal with homework assignments, have we? I’m okay with this Stackoverflow moderation policy proposal for example, and I wonder what other people think.
I generally agree with this Stackoverflow policy; in no small part because it is hard to discuss with people that do not try to go further than “please do my homework” demands.
We all want to help people who are trying hard, and those who are doing something a little bit different. But it’s hard to tell the former apart from somebody who wants help with their homework, and someone who wants their homework to be done for them.
And enough of us have been either educators, or lab assistants, that we recognize that doing someone’s homework for them doesn’t actually help them learn at all.
I think it would be really valuable to have a clear policy on these matters, so that people can just refer to it, instead of having to formulate a statement themselves.