Updating Recent Posts
An online post (defined by the record type post below) has a
title, content, and timestamp. A user is defined by a record type
with many fields (see below), but the important fields for this
problem are
old_posts: a list of posts in decreasing order of timestamp;recent_posts: a list of posts in decreasing order of timestamp.
These fields have the further property that every post in
old_posts is older than every post in recent_posts (i.e., their
timestamps are smaller).
type post = { title : string; content : string; timestamp : int; } type user = { username : string; email : string; time_joined : int; is_paid_user : bool; balance : int; next_payment_time : int; is_paused : bool; num_followers : int; num_likes : int; old_posts : post list; recent_posts : post list; }
Implement the function
val update_recent : user -> int -> int -> user
so that update_recent u time stale is a new user with the
following properties:
- every post in
recent_postsat leaststaletimesteps old (with respect to the current timetime) has been moved toold_posts; - the ordering property above is maintained: timestamps still
appear in decreasing order, and
old_postsis older thanrecent_posts.
Examples
let p t = {title="";content="";timestamp=t} let mk op rp = { username = "" ; email = "" ; time_joined = 0 ; is_paid_user = true ; balance = 0 ; next_payment_time = 0 ; is_paused = true ; num_followers = 0 ; num_likes = 0 ; old_posts = op ; recent_posts = rp ; } let _ = assert (update_recent (mk [] [p 30;p 20;p 10;p 0]) 50 30 = mk [p 20;p 10;p 0] [p 30])