2 Replies - 733 Views - Last Post: 14 April 2013 - 05:07 PM

#1 yeris  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 23-January 13

OCaml: Lazy List

Posted 14 April 2013 - 03:59 PM

Hi!
It's me again, trying to win a fight with OCaml and loosing really bad.
Now I'm working with Lazy Lists,
so this is my set-up
(*setup*)

type 'a lazy_list = LNil | LCons of 'a * (unit -> 'a lazy_list);;

let rec lfrom k = LCons (k, function () -> lfrom (k + 1));;

let rec ltake = function
      (0, xq) -> []
    | (n, LNil) -> []
    | (n, LCons (x, xf)) -> x :: ltake (n - 1, xf());;


and now I'm trying to make a function that takes a lazy list, and returns a lazy list with every element duplicated n times (exp.n=3 [1,2,3]->[1,1,1,2,2,2,3,3,3] but it should be lazy). So it's somthing like int -> 'a llist -> 'a llist.
After modifying other function, I have something like that:

let lreapt n llist1=
	let rec ljoin llist1 llist2 =
      match llist1 with
      | LNil -> llist2
      | LCons (x, xf) -> LCons (x, function () -> ljoin (xf()) llist2)
    in
      let rec lduplicate k item =
        match k with
        | 0 -> LNil
        | _ -> LCons (item, function () -> lduplicate (k-1) item)
      in
        match llist1 with
        | LNil -> llist1
        | LCons (x, xf) -> LCons (x, function () -> ljoin (lduplicate n x) (lrepeat n (xf())))



and an error: This expression has type int but an expression was expected of type
int -> int.

But i'm pretty sure, that not the error but the fact, that my idea is broken in some way, is the biggest issue.
I just can't get a grasp on functional programming.

Is This A Good Question/Topic? 0
  • +

Replies To: OCaml: Lazy List

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: OCaml: Lazy List

Posted 14 April 2013 - 04:44 PM

Quote

This expression has type int but an expression was expected of type
int -> int.


Which expression?

Quote

But i'm pretty sure, that not the error but the fact, that my idea is broken in some way


Your logic looks fine actually. So does your code in fact. Whatever the issue is, it's probably a small one.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: OCaml: Lazy List

Posted 14 April 2013 - 05:07 PM

I found your problem: You spelled your function name differently in the function definition than when you call it. You also need to add rec to the function as it is recursive.

So I'm assuming that the expression the error message was complaining about was the first argument to the recursive function call and that you got that particular error message (I got a different one when I tried to compile your code myself) because you've previously defined a version of the function without the misspelling that accepted an int -> int as its first parameter.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1