Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6210305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:08:13+00:00 2026-05-24T06:08:13+00:00

I am learning erlang and I stumbles over some behaviour I cannot quite understand.

  • 0

I am learning erlang and I stumbles over some behaviour I cannot quite understand. Take this piece of code. (I know there are existing libraries for what I am programming, but as I stated, I do this for educational purposes):

-module (codec).
-compile (export_all).
-record (node, {symbol, weight, order, left, right, parent} ).
-record (tree, {root, nodes} ).

highestOrderForWeight (Weight, Tree) ->
    lists:max ( [Node#node.order || Node <- Tree#tree.nodes, Node#node.weight == Weight] ).

swapMaybe (Node, Tree) ->
    case highestOrderForWeight (Node#node.weight, Tree) of
        Node#node.order -> pass; 
        Node#node.parent -> pass;
        Tree#tree.root -> pass;
        Partner -> io:format ("Swapping ~p with ~p.~n", [Node#node.order, Partner] )
    end.

The compiler is not at all amused about my code:

./so.erl:11: illegal pattern
./so.erl:12: illegal pattern
./so.erl:13: illegal pattern
error

It has appearently some trouble digesting records in patterns, because when I change my code to this clumsy work-around, it compiles fine:

swapMaybe2 (Node, Tree) ->
    [Order, Parent, Root] = [Node#node.order, Node#node.parent, Tree#tree.root],
    case highestOrderForWeight (Node#node.weight, Tree) of
        Order -> pass; 
        Parent -> pass;
        Root -> pass;
        Partner -> io:format ("Swapping ~p with ~p.~n", [Node#node.order, Partner] )
    end.

Questions:

  • How do I access record fields in patterns?
  • If it is not possible to do so, why is that so?
  • If it is not possible to do so, what is the common practice to work around that?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:08:13+00:00Added an answer on May 24, 2026 at 6:08 am

    Actually records are just a compile time syntactic sugar and you can look at the actual constructs by using 'E' compiler option. For example Node#node.order will be replaced by something like this:

    case Node of
        {node,_,_rec0,_,_,_} ->
            rec0;
        _ ->
            error({badrecord,node})
    end
    

    And of course when you try to use Node#node.order as a patter compiler reports illegal pattern for this construct.

    Your swapMaybe function can be rewritten like this:

    swapMaybe(#node{order=Order, parent=Parent}, Tree=#tree{root=Root}) ->
        case highestOrderForWeight (Weight, Tree) of
            Order -> pass; 
            Parent -> pass;
            Root -> pass;
            Partner -> io:format ("Swapping ~p with ~p.~n", [Order, Partner] )
        end.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started learning Erlang and am trying out some Project Euler problems
I've just started learning about thread safety. This is making me code a lot
learning html/css here. I can't seem to understand how to tweak this drop down
I am learning Erlang and am quite impressed how easy it is to parallelize
I'm working my way through some simple programs for learning Erlang, and whenever I
I'm in the process of learning Erlang. As an exercise I picked up the
Since a few months I've been learning Erlang, and now it was time to
Learning Scala currently and needed to invert a Map to do some inverted value->key
I am very new to Erlang and as part of my learning exercise, I
Learning Ruby. Needed to create a hash of arrays. This works... but I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.