This page is no longer maintained — Please continue to the home page at www.scala-lang.org

High Order Programming and Map Function

3 replies
Mücahit Şenol
Joined: 2011-11-27,
User offline. Last seen 42 years 45 weeks ago.

Hello,

I am learning Scheme/Racket in uni. I try to apply, what I learn, with
Scala. I am learning high-order programming recently. In Scheme you
can use map function on multiple lists, if lists have same length:

(map + list1 list2 list3 list4 list5 list6)

You can assume that list1...list6 are integer lists like (list 1 2
3).

It is possible in scala until 3 lists:

(List(1, 1, 1), List(1, 1, 1), List(1, 1, 1)).zipped.map(_+_+_)

But this gives an error:

(List(1, 1, 1), List(1, 1, 1), List(1, 1, 1), List(1, 1,
1)).zipped.map(_+_+_+_)

Why it is not possible? Secondly is there sum function which takes n
arguments (Scheme + is takes n arguments). I don't want to write like _
+_+_... that.

I try to use Scala in pure functional way because Haskell and Scheme
does not have big library and they are not fast enough.

missingfaktor
Joined: 2010-04-13,
User offline. Last seen 1 year 4 days ago.
Re: High Order Programming and Map Function


2011/11/27 Mücahit Şenol <mucahitsenol86@gmail.com>
Hello,

I am learning Scheme/Racket in uni. I try to apply, what I learn, with
Scala. I am learning high-order programming recently. In Scheme you
can use map function on multiple lists, if lists have same length:

(map + list1 list2 list3 list4 list5 list6)

You can assume that list1...list6 are integer lists like (list 1 2
3).

It is possible in scala until 3 lists:

(List(1, 1, 1), List(1, 1, 1), List(1, 1, 1)).zipped.map(_+_+_)

But this gives an error:

(List(1, 1, 1), List(1, 1, 1), List(1, 1, 1), List(1, 1,
1)).zipped.map(_+_+_+_)
 
Why it is not possible? Secondly is there sum function which takes n
arguments (Scheme + is takes n arguments). I don't want to write like _
+_+_... that.


That's because the method 'zipped' is only pimped on Tuple2 and Tuple3. Try this: 
List(List(1, 1, 1), List(1, 1, 1), List(1, 1, 1), List(1, 1, 1)).transpose.map(_.sum)
I try to use Scala in pure functional way because Haskell and Scheme
does not have big library and they are not fast enough.



vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: High Order Programming and Map Function
In addition to the previous answer: the principal difference is that Scala is typed, and Scheme is not; so in Scheme you  can basically write whatever you like, runtime will sort it out. You can add apples to oranges in Scheme.

Thanks,
-Vlad


2011/11/27 Mücahit Şenol <mucahitsenol86@gmail.com>
Hello,

I am learning Scheme/Racket in uni. I try to apply, what I learn, with
Scala. I am learning high-order programming recently. In Scheme you
can use map function on multiple lists, if lists have same length:

(map + list1 list2 list3 list4 list5 list6)

You can assume that list1...list6 are integer lists like (list 1 2
3).

It is possible in scala until 3 lists:

(List(1, 1, 1), List(1, 1, 1), List(1, 1, 1)).zipped.map(_+_+_)

But this gives an error:

(List(1, 1, 1), List(1, 1, 1), List(1, 1, 1), List(1, 1,
1)).zipped.map(_+_+_+_)

Why it is not possible? Secondly is there sum function which takes n
arguments (Scheme + is takes n arguments). I don't want to write like _
+_+_... that.

I try to use Scala in pure functional way because Haskell and Scheme
does not have big library and they are not fast enough.



nicolas.oury@gm...
Joined: 2011-02-13,
User offline. Last seen 42 years 45 weeks ago.
Re: High Order Programming and Map Function

2011/11/27 Vlad Patryshev :
> In addition to the previous answer: the principal difference is that Scala
> is typed, and Scheme is not; so in Scheme you  can basically write whatever
> you like, runtime will sort it out. You can add apples to oranges in Scheme.
>

More precisely: Scala is statically typed, Scheme is dynamically typed.
Scheme lets you write more program but with less correction warranty.

For the author of the initial mail:
If you want to have more experience with static typing, closer to your
background,
before diving into Scala, you can play a bit with Typed Racket.
http://docs.racket-lang.org/ts-guide/

Best,

Nicolas.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland