3 ms·
This is based on K. Another array language is J http://jsoftware.com http://jsoftware.com In J this would be: dot =: +/ . * P =: 2 3 4 Q =: 1 0 2 P
by gcanyon 3y ago
This is based on K. Another array language is J http://jsoftware.com http://jsoftware.com In J this would be:
dot =: +/ . *
P =: 2 3 4
Q =: 1 0 2
P dot Q
That will return 10, the dot product P and Q.
- eismcc 3y agoIn KlongPy, dot product is: dot::{+/x*y} P::[2 3 4] Q::[1 0 2] dot(P;Q)
- __marvin_the__ 3y agoTIL of KlongPy (and Klong). Looking at the project page, I think it looks really cool. https://pypi.org/project/klongpy/ https://pypi.org/project/klongpy/
- nils-m-holm 3y ago> dot(P;Q) You can also write P dot Q.
- abrudz 3y agoThe original array lang is APL: dot←+.× but why give it a name when spelling it out is as short as the shortest reasonable name for it (and then you might need to put spaces around the name).
- gcanyon 3y agoFor people who understand the language, there is little point to naming it. For illustrating to people unfamiliar with the language, it helps to break apart the "this is the thing I'm talking about" part from the "here's how you would use it" part.
- akavi 3y agoI guess I still fail to see the advantage of this over Haskell: dot = (sum.) . zipWith (\*) p = [2, 3, 4] q = [1, 0, 2] p `dot` q Like, the only difference to my eyes is using names for sum/zipWith, and not having the lifts/restructuring happen "magically"
- RodgerTheGreat 3y agoThe APL/J/K/Klong versions are more general; they all work for a variety of ranks of input, whereas the Haskell version- which is the longest and most visually noisy- bakes in a single rank's mapping explicitly.
- radiator 3y agoWell, for one, your Haskell version is obviously much longer.
- dr_kiszonka 3y agoHaha!