combining two streams, pulling element from one then other - sml

Given these declarations I am having trouble understanding streams in SML:
exception Bad of string;
fun from seed next = Cons(seed,fn () => from (next seed) next);
fun head (Nil) = raise Bad("got nil in head")
| head (Cons(a,b)) = a;
fun tail (Nil) = raise Bad("got nil in tail")
| tail(Cons(a,b)) = b();
fun take 0 stream = nil
| take n (Nil) = raise Bad("got nil in take")
|take n (Cons(h,t)) = h::(take (n-1) (t()));
I can create a stream of naturals [1.0,2.0,3.0...] like so: val nat = from 1.0 (fn x => x+1.0);
and a stream of ones: val one = from 1.0 (fn x => x);
But how would i go about creating a stream from these two streams? Specifically a function that merges two streams and outputs a stream.
something like: fun merge a b where a and b are streams. Ie if we did take 5 after merging these two it would give [1.0,1.0,2.0,1.0,3.0]

If this were a list, you would write
fun merge a b = Cons (head a, Cons (head b, merge (tail a) (tail b)))
and then you add a function indirection to each cons to "streamify" it:
fun merge a b = Cons (head a, fn () => Cons (head b, fn () => (merge (tail a) (tail b))))

Related

Generic Sequence over natural numbers

I am trying to create a generic sequence, that would behave the following:
val generic_sequence= fn : (int -> int) -> int seq
that is, It should receive as an input a function:
foo: int -> int
and create a sequence that activates foo on all natural numbers.
I wrote the following auxiliary code (works fine):
datatype 'a seq = Nil
| Cons of 'a * (unit-> 'a seq);
fun head (Cons(x,_)) = x;
fun tail (Cons (_,xf)) = xf();
fun naturals k = Cons(k,fn()=>naturals (k+1));
and when I tried implementing the generic sequence I got stuck.
This is where I've got.
fun aux (Cons(x,xf))= (Cons(foo x,(fn=>aux((xf())))));
fun generic_seq foo = (aux (from 0));
I have 2 problems:
It doesn't compile
I am not sure if my approach is correct
Would appreciate some help here.
Ok, I figured it out,
I created a mapq function and it basically did everything for me.
fun mapq f Nil = Nil
| mapq f (Cons (x,xf)) = Cons (f(x), fn() => mapq f (xf()));

SML with lazy list function

I'm trying to make a function which can return the specific nth element of lazylist.
Here is what I made:
datatype 'a lazyList = nullList
| cons of 'a * (unit -> 'a lazyList)
fun Nth(lazyListVal, n) = (* lazyList * int -> 'a option *)
let fun iterator (laztListVal, cur, target) =
case lazyListVal of
nullList => NONE
| cons(value, tail) => if cur = target
then SOME value
else iterator (tail(), cur+1, target)
in
iterator(lazyListVal,1,n)
end
I expected the result that as recusing proceeds, eventually the variable cur gets same as the variable target, and then the function iterator returns SOME value so it will return the final nth element.
But when I compile it and run, it only returns the very first element however I test with the lazylist objects.
Please figure what is the problem. I have no idea...
cf) I made another function which is relevant to this problem, the function that transforms lazylist into SML original list containing the first N values. Codes above:
fun firstN (lazyListVal, n) = (* lazyList * int -> 'a list *)
let fun iterator (lazyListVal, cur, last) =
case lazyListVal of
nullList => []
| cons(value, tail) => if cur = last
then []
else value::iterator(tail(),cur+1,last)
in
iterator(lazyListVal,0,n)
end
The strange thing is the function firstN is properly working.
The problem is that your iterator function does case lazyListVal of ..., but the recursive tail is called laztListVal, so for every iteration, it keeps looking at the first list. Use better variable names to avoid this kind of "invisible" bug.
For a simpler definition of nth:
datatype 'a lazyList = NullList | Cons of 'a * (unit -> 'a lazyList)
fun nth (NullList, _) = NONE
| nth (Cons (x, xs), 0) = SOME x
| nth (Cons (_, xs), n) = nth (xs (), n-1)
val nats = let fun nat n = Cons (n, fn () => nat (n+1)) in nat 0 end
val ten = nth (nats, 10)
Edit: While function pattern matching is ideal here, you could also have used a case ... of ... here. A helper function seems unnecessary, though, since you can simply use the input argument n as the iterator:
fun nth (L, n) =
case (L, n) of
(NullList, _) => NONE
| (Cons (x, xs), 0) => SOME x
| (Cons (_, xs), n) => nth (xs (), n-1)
You may however want to make the function more robust:
fun nth (L, n) =
let fun nth' (NullList, _) = NONE
| nth' (Cons (x, xs), 0) = SOME x
| nth' (Cons (_, xs), n) = nth' (xs (), n-1)
in if n < 0 then NONE else nth' (L, n) end
Here having a helper function ensures that n < 0 is only checked once.
(You could also raise Domain, since negative indices are not well-defined.)

Get Last element in a list in sml foldl

I am trying to retrieve the last element of a list using foldl in SML, and not getting a lot of correct answers from what i have done already, the following is one of my bad tries, any help!
fun last p = foldr ( fn (x, y) => if ( p y ) then x else y ) [];
You could use List.last even though you should be careful, as it will raise an exception if you use it on an empty list.
For the MosML implementation, you can find the documentation at http://mosml.org/mosmllib/List.html#last-val
As RasmusWL mentioned, List.last is the appropriate tool for general use.
It can be recreated using foldl:
fun last(alist, init) = List.foldl (fn(x,y) => x) init alist
This produces a signature: fn : 'a list * 'a -> 'a
To write it with foldr use:
fun last(alist, init) = List.foldr (fn(x,y) => x) init (List.rev alist)
An advantage of using foldl is that it does not require a check for an empty list:
List.foldl (fn(x,y) => x) 0 [];
(* val it = 0 : int *)

Less-than function in Standard ML

I'm trying to make a function in SML that takes a list and an int and returns a list of all elements that are less than the int int * int list -> int list I wrote the following code:
- fun less (e, L) =
= map (fn a => if a < e then a else []) L;
also with the following code it doesnt work also:
- fun less (e, L) =
= map (fn a => if a < e then a) L;
and the error im getting is :
stdIn:22.15-22.38 Error: types of if branches do not agree [overload]
then branch: 'Z
else branch: 'Y list
in expression:
if a < e then a else nil
I think the problem is with the else part but I dont know what to put in it to work, anybody have any suggestion? I should use either map, foldl, or foldr functions.
EDIT:
- fun less (e, L) =
= let
= val acc = []
= in
= foldr (fn a => if a < e then a::acc else acc) acc L
= end;
still gives me error, the following error :
stdIn:241.3-241.54 Error: operator and operand don't agree [overload]
operator domain: 'Z * 'Y -> 'Y
operand: 'X -> 'X list
in expression:
foldr (fn a => if <exp> < <exp> then <exp> :: <exp> else acc)
The error message is clear; since a has type int and [] has type 'a list, their types are mismatched.
The problem is you chose the wrong high-order function for the task. The filter on List structure is best suited here:
fun less (e, L) = filter (fn a => a < e) L
You could use recursion to implement less explicitly, or use foldl/foldr to accumulate filtered lists. However, map seems irrelevant here.
EDIT:
I will give a hint about using foldl/foldr. You start with empty list as the accumulator. Prepend an element to the accumulator whenever that element is smaller than e; otherwise, return the accumulator.
EDIT 2:
You forgot to pass acc as an argument in the lambda function:
fun less (e, L) = foldr (fn (a, acc) => if a < e then a::acc else acc) [] L
And the let..in..end part is redundant because you use [] as the accumulator only.

Ocaml List: Implement append and map functions

I'm currently trying to extend a friend's OCaml program. It's a huge collection of functions needed for some data analysis.. Since I'm not really an OCaml crack I'm currently stuck on a (for me) strange List implementation:
type 'a cell = Nil
| Cons of ('a * 'a llist)
and 'a llist = (unit -> 'a cell);;
I've figured out that this implements some sort of "lazy" list, but I have absolutely no idea how it really works. I need to implement an Append and a Map Function based on the above type. Has anybody got an idea how to do that?
Any help would really be appreciated!
let rec append l1 l2 =
match l1 () with
Nil -> l2 |
(Cons (a, l)) -> fun () -> (Cons (a, append l l2));;
let rec map f l =
fun () ->
match l () with
Nil -> Nil |
(Cons (a, r)) -> fun () -> (Cons (f a, map f r));;
The basic idea of this implementation of lazy lists is that each computation is encapsulated in a function (the technical term is a closure) via fun () -> x.
The expression x is then only evaluated when the function is applied to () (the unit value, which contains no information).
It might help to note that function closures are essentially equivalent to lazy values:
lazy n : 'a Lazy.t <=> (fun () -> n) : unit -> 'a
force x : 'a <=> x () : 'a
So the type 'a llist is equivalent to
type 'a llist = 'a cell Lazy.t
i.e., a lazy cell value.
A map implementation might make more sense in terms of the above definition
let rec map f lst =
match force lst with
| Nil -> lazy Nil
| Cons (hd,tl) -> lazy (Cons (f hd, map f tl))
Translating that back into closures:
let rec map f lst =
match lst () with
| Nil -> (fun () -> Nil)
| Cons (hd,tl) -> (fun () -> Cons (f hd, map f tl))
Similarly with append
let rec append a b =
match force a with
| Nil -> b
| Cons (hd,tl) -> lazy (Cons (hd, append tl b))
becomes
let rec append a b =
match a () with
| Nil -> b
| Cons (hd,tl) -> (fun () -> Cons (hd, append tl b))
I generally prefer to use the lazy syntax, since it makes it more clear what's going on.
Note, also, that a lazy suspension and a closure are not exactly equivalent. For example,
let x = lazy (print_endline "foo") in
force x;
force x
prints
foo
whereas
let x = fun () -> print_endline "foo" in
x ();
x ()
prints
foo
foo
The difference is that force computes the value of the expression exactly once.
Yes, the lists can be infinite. The code given in the other answers will append to the end of an infinite list, but there's no program you can write than can observe what is appended following an infinite list.