0% found this document useful (0 votes)
6 views1 page

Solution Ocaml

Uploaded by

кана л
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

Solution Ocaml

Uploaded by

кана л
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

(* task 1 *)

let rec fib n =


match n with
| 0 -> 0
| 1 -> 1
| _ -> fib (n - 2) + fib (n - 1)
;;

let fibTail n =
let rec aux n a b =
match n with
| 0 -> a
| _ -> aux (n - 1) b (a + b)
in aux n 0 1
;;

fib 42;;
fibTail 42;;

(* task 2 *)
let root3 a =
let precision = 1e-15 in
let rec aux x =
if abs_float (x *. x *. x -. a) <= precision *. abs_float a then x
else aux(x +. (a /. (x *. x) -. x) /. 3.)
in aux (if a > 1. then a /. 3. else a)
;;

root3 (-216.);;

(* task 3 *)
let [_; _; x; _; _] = [-2; -1; 0; 1; 2];;
let [(_, _); (x, _)] = [(1, 2); (0, 1)];;

(* task 4 *)
let rec initSegment (xs, ys) =
match (xs, ys) with
| ([], _) -> true
| (_, []) -> false
| (x_head :: x_tail, y_head :: y_tail) ->
if x_head = y_head then initSegment (x_tail, y_tail)
else false
;;

initSegment ([1; 2; 3], [1; 2; 3; 4; 5]);;

(* task 5 *)
let rec replaceNth list n x =
match (list, n) with
| ([], _) -> []
| (_ :: tl, 0) -> x :: tl
| (hd :: tl, _) -> hd :: replaceNth tl (n - 1) x
;;

replaceNth ['o';'l';'a'; 'm'; 'a'; 'k'; 'o'; 't'; 'a'] 1 's';;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy