List Info

Thread: "ocaml_beginners"::[] constant functions?




"ocaml_beginners"::[] constant functions?
user name
2006-12-28 01:32:21

I'm trying to learn Haskell and translating some Lisp
functions as exercises.

How would I write a Haskell function named ALWAYS that
behaves like this:

one = always 1
bozo = always "clown"

> map one [2,3,4,5,6]
[1,1,1,1,1]

> one 62
1

> map bozo [2,3,4,5,6]
["clown","clown","clown","clown","clown"]

> bozo 62
"clown"

i.e. ALWAYS returns a function with a single parameter
that is ignored, returning instead the value given to
ALWAYS when the function was created.

This is what I've been trying:

always :: (a -> a) -> a -> a
always x = (y -> x)

one = always 1

Michael

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] constant functions?
user name
2006-12-28 02:38:40

On Thursday 28 December 2006 01:32, michael rice wrote:
> i.e. ALWAYS returns a function with a single parameter
> that is ignored, returning instead the value given to
> ALWAYS when the function was created.

Given that you've posted this to an OCaml list, I'm going to assume that you
want OCaml and not Haskell.

I don't think you can do that in OCaml due to the value restriction:

# let always x _ = x;;
val always : 'a -> 'b -> 'a
# let one = always 1;;
val one : '_a -> int

Note that '_a denotes a monomorphic type, required due to the formation
of "one" from a partial application.

Instead you must write explicitly:

# let one y = always 1 y;;
val one : 'a -> int

in which case you might as well write:

# let one _ = 1;;
val one : 'a -> int

--
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] constant functions?
user name
2006-12-28 02:50:49

My bad. Too many lists. Still it's nice to know how to
do it with Ocaml.

Thanks,

Michael

--- Jon Harrop < jon%40ffconsultancy.com">jonffconsultancy.com&gt; wrote:

> On Thursday 28 December 2006 01:32, michael rice
> wrote:
&gt; > i.e. ALWAYS returns a function with a single
&gt; parameter
> > that is ignored, returning instead the value given
>; to
> > ALWAYS when the function was created.
>
> Given that you've posted this to an OCaml list, I'm
> going to assume that you
> want OCaml and not Haskell.
>
> I don't think you can do that in OCaml due to the
> value restriction:
>
> # let always x _ = x;;
> val always : 'a -> 'b -> 'a
> # let one = always 1;;
> val one : '_a -> int
>
> Note that '_a denotes a monomorphic type, required
> due to the formation
> of "one&quot; from a partial application.
>
> Instead you must write explicitly:
>
> # let one y = always 1 y;;
> val one : 'a -> int
>
> in which case you might as well write:
&gt;
> # let one _ = 1;;
> val one : 'a -> int
>
> --
> Dr Jon D Harrop, Flying Frog Consultancy Ltd.
> Objective CAML for Scientists
>
http://www.ffconsultancy.com/products/ocaml_for_scientists
>

__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

__._,_.___
.

__,_._,___
[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )