Map function listed in standard ML

Based on this definition:

A list of additions is a (simple) implementation of the abstract type of the list, which makes construction cheap (O (1)), but makes destruction costly (O (n)). Types 'a alistNNand are 'a alistdefined as follows:

datatype 'a alistNN = Sing of 'a | Append of 'a alistNN * 'a alistNN
datatype 'a alist = Nil | NonNil of 'a alistNN

A type 'a alistNNrepresents a non-nil add-on list, and a type 'a alistrepresents an arbitrary (nil or non-nil) add-on list.

I was asked to create a map function defined as:

fun alistMap (f: 'a -> 'b) (xs: 'a alist): 'b alist =

Runs the map in the add list.

I defined it as follows:

fun alistMap (f: 'a -> 'b) (xs: 'a alist): 'b alist =
    case xs of
      Nil => Nil
    | NonNil xs => 
      let
        fun mapNN(f: 'a -> 'b) (xs: 'a alist): 'b alist =
           case xs of 
             Sing x => Sing (f x)
           | Append (ys, zs) => 
             let
               val ws = mapNN f ys
               val ts = mapNN f zs
             in
               alistAppend (ys , zs)
             end
      in
        mapNN f xs
      end

I keep running into types, especially with

Sing x => Sing (f x)

Any idea what could be causing this?

+4
source share
1 answer

mapNN . Sing Append alistNN, alist. .

fun mapNN (f : 'a -> 'b) (xs : 'a alistNN) : 'b alistNN = ...

:

  • alistAppend (ys, zs) 'a alist, - 'b alistNN, . , ws ts, ...;)

  • mapNN mapNN f xs, 'b alistNN, - 'b alist.

, alist alistNN. , !

+2

Source: https://habr.com/ru/post/1694663/


All Articles