Question: let makeStream this state next = ( ( this , state ) , next ) ;; ( * FIRST. Return the first element of a

let makeStream this state next =
((this, state), next) ;;
(* FIRST. Return the first element of a stream. *)
let first ((this, state), next)=
this ;;
(* REST. Return a stream with its first element removed. *)
let rest ((this, state), next)=
(next this state, next) ;;
(* TAKE. Return a list of the first COUNT elements TAKEn from STREAM. *)
let rec take count stream =
match count
with 0->[]|
_->(first stream) :: take (count -1)(rest stream) ;;
(* NATURALS. A infinite stream of 0,1,2,3,4,5,6,7,8,9... We don't use
STATE here, so we let it be the dummy unit object ().*)
let naturals =
makeStream 0()(fun this state ->(this +1,())) ;;
let odds =
let rec generate_odd n = makeStream n (n +2)(fun this state ->(this + state, state +2)) in
generate_odd 1 ;;
let rec trim count stream =
match count with
|0-> stream
|_-> trim (count -1)(rest stream) ;;
let rec scale factor stream =
let newThis = first stream * factor in
let newState =(factor, rest stream) in
makeStream newThis newState (fun (_, restState)-> scale factor restState)
let rec sum left right =
let combine (stream1, stream2)=(first stream1+ first stream2,(rest stream1, rest stream2)) in
makeStream (combine (left, right))(combine)(fun __-> sum (rest left)(rest right)) ;;
My error is a type mismatch error at:
let rec scale factor stream =
let newThis = first stream * factor in
let newState =(factor, rest stream) in
makeStream newThis newState (fun (_, restState)-> scale factor restState)
55
makeStream newThis newState (fun (
expression has type(int *(int *('a *(int ->'b ->'a))))*('c *((int *'b)*(int ->>'b ->>'a))->d)
but an expression was expected of type 'd
The type variable 'd occurs inside
(int *(int *('a *(int ->'b ->'a))))*('c *((int *'b)*(int ->>'b ->>'a))->d)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!