Gwen Wong
Gwen Wong

Reputation: 357

Using nested functions

I have problem: Write a function called minimum6 that takes 6 arguments and returns the smallest one. Example: minimum6 10 20 30 40 50 60 = 10

and this is what i got so far:

let min a b = if a < b then a else b;; let minimum6 x1 x2 x3 x4 x5 x6 = min (min (min (min (min x1 x2) x3) x4) x5) x6);;

however, i get 'This expression has type int but an expression was expected of type 'a -> 'b' pointing to the first '10' I'm new to the language, what did i do wrong? was it because i used a function in a function?

Upvotes: 2

Views: 111

Answers (1)

seanmcl
seanmcl

Reputation: 9946

You have some parentheses problems. :)

You could also do something more general. Something like:

let rec list_min min = function
  | [] -> min
  | x::xs -> if x < min then list_min x xs else list_min min xs

let min6 x1 .. x6 = list_min x1 [x2; ...; x6]

Upvotes: 1

Related Questions