recursion - Creating own substing functions recursively in Ocaml -



recursion - Creating own substing functions recursively in Ocaml -

how can write substring function in ocaml without using assignments lists , iterations, recursions? can utilize string.length.

i tried far

let substring s s2 start stop= if(start < stop) substring s s2 (start+1) stop else s2;;

but wrong, problem how can pass string beingness built gradually recursive calls?

this feels homework problem intended teach think think recursion. me easier think recursion part if decide on basic operations you're going use. can't utilize assignments, lists, or iterations, okay. need extract parts of input string somehow, can't utilize built-in substring function this, defeat purpose of exercise. other operation can think of 1 extracts single character string:

# "abcd".[2];; - : char = 'c'

you need way add together character string, giving longer string. you're not allowed utilize assignment this. seems me you're going have utilize string.make translate character string:

# string.make 1 'a';; - : string = "a"

then can concatenate 2 strings using ^ operator:

# "abc" ^ "def" - : string = "abcdef"

are allowed utilize these 3 operations? if so, can start thinking recursion part of substring problem. if not, don't understand problem plenty yet give advice. (or maybe whoever set restrictions didn't expect have calculate substrings? restrictions kind of hint how should proceed.)

moving on specific question. in origin fp programming, don't want pass reply downwards recursive calls. want pass smaller problem downwards recursive call, , reply back it. substring problem, illustration of smaller problem inquire substring starts 1 character farther along in containing string, , 1 character shorter.

(later on, might want pass partial answers downwards recursive calls in order tail-recursive behavior. don't worry now.)

recursion ocaml substring

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -