Deciphering Scala Errors When Returning Functions From Functions

muke

I'm trying to build a basic combinator parser in Scala and when trying to compile the most basic parser with it (the parser of the letter 'A') I get three syntax errors from scalac which I don't understand. I'm looking at my code and can't figure out which parts of the syntax are incorrect, even when comparing with examples online. Could someone please explain what parts of my Scala code are wrong here?

Code:

import lexer
import scala.Option

object Main {
  def main(args: Array[String]) {
    val Lexer = new lexer.Lexer
    val tokens = Lexer.lex(args(0))
    val parseA = satsify(t => t.key == "A")
    println(parseA(tokens))
  }

  def satsify(predicate: Token => Bool): List(Token) => Option[(Token, List(Token))] = {
    tl: List(Token) => match tl { 
        case tl.isEmpty => None 
        case predicate(tl(0)) => Some(tl(0), tl.tail)
        case _ => None
    }
  }
}

Errors:

combParser.scala:2: error: '.' expected but ';' found.
import scala.Option
^
combParser.scala:12: error: '=' expected but '(' found.
  def satsify(predicate: Token => Bool): List(Token) => Option[(Token, List(Token))] = {
                                             ^
combParser.scala:19: error: illegal start of simple expression
}
^
three errors found
Mario Galic

There are quite a few syntax errors, mainly

  • type constructor takes a type parameters using square brackets, thus List[Token] instead of List(Token)
  • guards inside pattern matching cannot be used directly, thus case v if predicate(tl(0)) => instead of case predicate(tl(0)) =>

Taking above into account try

  def satsify(predicate: Token => Boolean): List[Token] => Option[(Token, List[Token])] = {
    (tl: List[Token]) => tl match {
      case Nil => None
      case head :: tail if predicate(head) => Some(head, tail)
      case _ => None
    }
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Returning values from functions when efficiency matters

Returning functions from functions in JavaScript

Javascript functions - deciphering the documentation about basic functions

Returning from /bin/sh Functions

Returning values from functions in Python

returning multiple values from functions

Scala code returning error with avg an abs functions

What are the best practices for returning one of mulitple errors from functions and ending the program in C?

when do Scala functions execute

crypto decipher returning unknown characters when deciphering from base64 to utf-8

returning functions in R - when does the binding occur?

Mysqli not returning a value when i use functions

How to test "functions returning errors" using Jest and Typescript?

Passing structs to functions and returning structs from functions in Zig

TypeScript typing errors when piping/composing functions

Repeated errors when passing arguments in functions

returning values from decorated functions in python

Returning constrained generics from functions and methods

Question about returning pointers from functions

Returning unique_ptr from functions

Issue with returning JArray from Azure Functions HttpTrigger

Python - Returning multiple functions from closures

Returning promises from Synchronous JS Functions

Returning a concatenated string from a functions' properties in javascript

Returning values from functions in structures in Visual Basic

Returning values from nested functions - JavaScript

Returning array of functions from custom react hook?

gracefully handling errors from suspend functions

Catching errors from nested async/await functions