Scala to Haskell

I previously wrote about switching my learning efforts from Haskell to Scala.  I've switched back. ;) It was a combination of things.  ...

July 22, 2020

Scala to Haskell

I previously wrote about switching my learning efforts from Haskell to Scala.  I've switched back. ;)

It was a combination of things.  I was enjoying Scala, but had this nagging feeling about being able to fall back to OOP concepts if I couldn't figure out how to do things in FP style.  This was nagging me as I was working on my ray tracer.  I was enjoying the book "Functional Programming Simplified" though.  I'll go back an finish that at some point.

A few weeks ago, a coworker asked me to do a lunch and learn presentation that I had once said I'd do.  Well, that was around a year ago that I said I'd do something on Haskell.  Then, holidays happened, there were scheduling difficulties and the whole pandemic started.  In the intervening months, I had switched to Scala.  So, I said "yes" anyway.

I started trying to prepare a L&L presentation on Haskell and realized that I remembered some things and that I had not given Haskell enough energy to learn.  This started me back on Haskell with fresh eyes.

I've started a ray tracer, of course.  I'm liking it more this time.  I'm forced to think harder and dive into Haskell libraries and pure FP techniques.

So, I now to finish the book and ray tracer this time, no matter what.  Then I can move on to another project.  Maybe a bowling kata?  We'll see.

March 9, 2020


Work has finally slowed down enough that I have inched my Scala based ray tracer along, just a tiny bit.

I'm keeping the code on gitlab this time, and so far it has been different, but problem free.  CI is built in on GitLab, but it did take a bunch of fiddling due to me not understanding a bunch of stuff, and out of date docs (of course).  So, on the CI side of things, I think it will run my tests on push now. Next up will be to enable Coveralls, "just because".

Now that I have that going, I started flipping through the book a little bit again.

The first step (after reading) is to get the tests written.  Luckily there are files full of features to start with. So, that's exactly what I'm going to do using ScalaTest. It supports multiple styles, but to stay with the style in the book, I'm going to use FeatureSpec.

Itt is fairly straightforward at the top-most level to get the tests in a format ScalaTest likes:

import org.scalatest.FeatureSpec         
import org.scalatest.GivenWhenThen               

class TupleFeatures extends FeatureSpec with GivenWhenThen {
  feature("Tuples, Vectors, and Points") {

    scenario("A tuple with w=1.0 is a point") {
      Given("a ← tuple(4.3, -4.2, 3.1, 1.0)")
      Then("a.x = 4.3")               
      And(" a.y = -4.2")                      
      And(" a.z = 3.1")              
      And(" a.w = 1.0")                                  
      And(" a is a point")      
      And(" a is not a vector")  

So, the next step there is to figure out where and how you write the background steps that set things up for you.

While flipping through the book again, it started off with Tuples. I thought I knew what to do but there is a slight rub.  The book uses four values (x, y, z, w) where the "thing" is a point of w = 1.0 and a vector if w = 0.0. I may be able to do something with the type system to handle this, I'm not sure.

My first thought was to not try to combine them at all, so after some failed attempts and a StackOverflow question, I ended up going down this road:

package com.craigtreptow.scrayz

package object types {
  final case class X(v: Double) extends AnyVal
  final case class Y(v: Double) extends AnyVal
  final case class Z(v: Double) extends AnyVal
  final case class Point(x: X, y: Y, z: Z)

package com.craigtreptow.scrayz.types

object Main extends App {
  val x: X = X(1.1)
  val y: Y = Y(2.2)
  val z: Z = Z(3.3)
  val p: Point = Point(x, z, z)


We'll see if this works out, but, hey, it's a place to start.