Home > Archive > Functional > June 2007 > higher order functions
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
higher order functions
|
|
| redeyeboy@gmail.com 2007-05-30, 10:06 pm |
| I was able to write all these without using higher order functions but
when using them i have the following problems!
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
val dual = foldr (fn (x:int,y) => x::x::y) nil;
how can i feed in any kind of list like int string or so. right now if
i take the ":int" off it's complaining . i was able to write such
without using foldr that accept any kind of input but i am stuck in
the case of using foldr.
here is the other function i am having trouble with.
in this one where am i trying to write the equivalent of sml implode i
can only put in string lists
how i tried str(x)^str(y) but that doesn't work. how do i change that.
so i can put in a list of chars and get the string back
val myimp = foldr (fn (x,y) => (x)^(y)) "";
in this last one i am trying to extract all the elems that are
multipls of 2,
val multiplesof2 = fn xs => map ( fn (x:int) => (if (x mod 2 = 0) then
x else 0 ) ) xs;
it recognizes the qualified ones and sets the others to 0 how can
change this so i just have the qualified ones without the 0's in the
list. sml doesn't let me to leave the else expression empty!
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------
| |
| redeyeboy@gmail.com 2007-05-30, 10:06 pm |
| by the way i am doing this in sml/nj. language by the way
| |
| Joachim Durchholz 2007-05-30, 10:06 pm |
| redeyeboy@gmail.com schrieb:
> how can i feed in any kind of list like int string or so.
If you wish to create a list that contains both strings and ints: that's
not possible, all elements of a list must have the same element.
(You can recreate the effects of non-uniformly-typed lists using
higher-order functions.)
Regards,
Jo
| |
| red_eye 2007-05-30, 10:06 pm |
| what i meant was that i how can my function accept different type of
lists as input for example:
a list of int [1,2,3] or string ["1","2","3"] where each input list
has the same type and not mixed types. so when i input for example
dual["*","^"] i get back ["*","*","^","^"]
| |
| red_eye 2007-05-30, 10:06 pm |
| what i meant was that i how can my function accept different type of
lists as input for example:
a list of int [1,2,3] or string ["1","2","3"] where each input list
has the same type and not mixed types. so when i input for example
dual["*","^"] i get back ["*","*","^","^"]
| |
| Mark T.B. Carroll 2007-05-30, 10:06 pm |
| red_eye <redeyeboy@gmail.com> writes:
> what i meant was that i how can my function accept different type of
> lists as input for example:
> a list of int [1,2,3] or string ["1","2","3"] where each input list
> has the same type and not mixed types. so when i input for example
> dual["*","^"] i get back ["*","*","^","^"]
If it's of any use, in Haskell I'd do:
Prelude> concatMap (replicate 2) [1..5]
[1,1,2,2,3,3,4,4,5,5]
Prelude> concatMap (replicate 2) "hello world"
"hheelllloo wwoorrlldd"
A simple version of dual would be,
dual [] = []
dual (x:xs) = x : x : dual xs
I don't know how easily you can turn that to ML - it's years since I
used it.
(I hope I'm guessing dual's intended behaviour correctly.)
-- Mark
| |
| red_eye 2007-05-30, 10:06 pm |
| yeah, the simple version that i came up with is the same, but i am
having trouble rewrting it using foldr, i want to generalize my
function so it will accept any list type,
thx though
| |
| Joachim Durchholz 2007-05-30, 10:06 pm |
| red_eye schrieb:
> yeah, the simple version that i came up with is the same, but i am
> having trouble rewrting it using foldr, i want to generalize my
> function so it will accept any list type,
Tried dropping type annotations and look what type SML assigns to it?
SML should automatically infer the most general type for you, and
whatever compiler/interpreter you use, it should be able to give you
that information.
Regards,
Jo
(P.S.: No need to respond to me by email, that would be just useless
duplicates in my inbox.)
| |
| Neelakantan Krishnaswami 2007-05-30, 10:06 pm |
| In article <<1180550731.530124.266050@j4g2000prf.googlegroups.com>>,
redeyeboy@gmail.com <redeyeboy@gmail.com> wrote:
>
> val dual = foldr (fn (x:int,y) => x::x::y) nil;
>
> how can i feed in any kind of list like int string or so. right now if
> i take the ":int" off it's complaining . i was able to write such
> without using foldr that accept any kind of input but i am stuck in
> the case of using foldr.
Try this:
val dual = fn list => (foldr (fn (x,y) => x :: x :: y) nil list)
The reason you are getting an error message is called the "value
restriction".
ML can only assign a polymorphic type to values, not arbitrary
expressions. This is because an arbitrary expression can have
side-effects, and giving a side-effecting expression a polymorphic
type can make the type system unsafe.
So this means that when you're defining a polymorphic function, like
your dual example, you need to give it as a value, rather than an
expression. This means turning it into a lambda:
val dual = fn list => (foldr (fn (x,y) => x :: x :: y) nil list)
> here is the other function i am having trouble with. in this one where
> am i trying to write the equivalent of sml implode i can only put in
> string lists
>
> how i tried str(x)^str(y) but that doesn't work. how do i change that.
> so i can put in a list of chars and get the string back
>
> val myimp = foldr (fn (x,y) => (x)^(y)) "";
The reason for this one is that the append function expects both
of its arguments to be strings:
op^ : string * string -> string
and the x argument to your folding function is a char. You can fix
this by using the Char.toString function:
fun myimp list = foldr (fn (c, s) => (Char.toString c) ^ s) "" list
> in this last one i am trying to extract all the elems that are
> multipls of 2,
>
> val multiplesof2 = fn xs => map ( fn (x:int) => (if (x mod 2 = 0) then
> x else 0 ) ) xs;
>
> it recognizes the qualified ones and sets the others to 0 how can
> change this so i just have the qualified ones without the 0's in the
> list. sml doesn't let me to leave the else expression empty!
The map function takes a function and a list and applies the function
to every element in the list. So the result will always have the same
length. To remove some of the elements, you can use the filter
function:
List.filter : ('a -> bool) -> 'a list -> 'a list
If you give filter a predicate, then it will return all of the
elements for which the predicate holds. This lets you define multiplesOf2
as:
fun multiplesOf2 list = List.filter (fn x => x mod 2 = 0) list
--
Neel R. Krishnaswami
neelk@cs.cmu.edu
| |
| rossberg@ps.uni-sb.de 2007-06-01, 10:07 pm |
| On 30 Mai, 23:40, Neelakantan Krishnaswami <n...@cs.cmu.edu> wrote:
>
> You can fix this by using the Char.toString function:
>
> fun myimp list = foldr (fn (c, s) => (Char.toString c) ^ s) "" list
Just an aside: I think the function you actually want here is
str : char -> string
The function Char.toString has a different purpose: as all toString
functions in the SML library it produces a string presentation that
conforms to SML syntax. In the case of Char.toString this means that
it performs quoting where necessary:
- str #"\n";
val it : string = "\n"
- Char.toString #"\n";
val it : string = "\\n"
That's the reason why there also is a function String.toString, btw.
- Andreas
| |
| Neelakantan Krishnaswami 2007-06-01, 10:07 pm |
| In article <<1180720496.822553.264100@p77g2000hsh.googlegroups.com>>,
rossberg@ps.uni-sb.de <rossberg@ps.uni-sb.de> wrote:
> On 30 Mai, 23:40, Neelakantan Krishnaswami <n...@cs.cmu.edu> wrote:
>
> Just an aside: I think the function you actually want here is
>
> str : char -> string
Yes, thanks for the correction!
--
Neel R. Krishnaswami
neelk@cs.cmu.edu
| |
|
|
|
|
|