Programming Praxis – Minimum Scalar Product

In today’s Programming Praxis exercise, our goal is to calculate the minimum scalar product of two vectors. Let’s get started, shall we?

A quick import:

import Data.List

Initially, I wrote a version that calculated all possible permutations and took the minimum value. After looking at the provided solution it turns out this isn’t necessary, which leads to the following shorter and more efficient version:

minScalar :: (Ord a, Num a) => [a] -> [a] -> a
minScalar a = sum . zipWith (*) (sort a) . reverse . sort

A test to see if everything is working properly:

main :: IO ()
main = print $ minScalar [1,3,-5] [-2,4,1] == -25
About these ads

Tags: , , , , , , , , ,

2 Responses to “Programming Praxis – Minimum Scalar Product”

  1. Mark Wotton Says:

    link to programmingpraxis is broken – is relative rather than absolute

  2. Remco Niemeijer Says:

    It should be working now. Thanks for the heads up.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: